Reputation: 59
I am trying to write a recursion where a word is trying mirror itself (appleelppa). MY thought process is to with a recursion that prints out the word in reverse order and then add the word in the beginning. However, this did not work somehow. here is my code,
public static String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return str + reverse(str.substring(1)) + str.charAt(0);
}
this is the output: ellepplepppleaapple
Any help?
Thanks
Upvotes: 0
Views: 1238
Reputation: 40036
Another not ugly recursive approach:
public static String mirror(String s) {
if (s == null || s.isEmpty()) {
return "";
}
return s.charAt(0) + mirror(s.substring(1)) + s.charAt(0);
}
In brief, mirroring a string means having a string with the first character, then the mirrored remainings, and then the first character again.
(Edit: just updated with actual code that I have tested which works)
Upvotes: 0
Reputation: 75555
It would be much simpler if you just wrote a recursive function to reverse the string, and then added it to the original.
public class Reverse{
public static String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return reverse(str.substring(1)) + str.charAt(0);
}
public static void main(String[] args) {
System.out.println("appleelppa" + reverse("appleelppa"));
}
}
The ugly hack to use exactly how one method with recursion and not iteration. Note that this is not the preferred way to do things. It is only placed here to show the OP that it can be done, since he was curious. This is inspired by a comment from AdrianShum.
public class Reverse{
public static String mirror(String str, boolean firstCall) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
if (firstCall)
return str + mirror(str.substring(1), false) + str.charAt(0);
else
return mirror(str.substring(1),false) + str.charAt(0);
}
public static void main(String[] args) {
System.out.println(mirror("appleelppa", true));
}
}
Upvotes: 0
Reputation: 2064
All the answers you were given gave you new solutions. I'm here to tell you your solution is 90% correct.
Remove str +
from:
return str + reverse(str.substring(1)) + str.charAt(0);
To make:
return reverse(str.substring(1)) + str.charAt(0);
And your original function works like a charm ;)
Upvotes: 0
Reputation: 201439
Your reverse
routine is almost correct (but you really should add a mirror
routine, your method is confused as is). You want something like this,
// Reverse the input String str.
private static String reverse(String str) {
// This just looked ugly.
if (str == null || str.length() <= 1) {
return str;
}
// this is how you recursively reverse the word.
return reverse(str.substring(1)) + str.charAt(0);
}
// mirror is trivial, the word and the reverse of the word.
public static String mirror(String str) {
return str + reverse(str);
}
public static void main(String[] args) {
String str = "apple";
System.out.println(mirror(str));
}
Output is (as requested)
appleelppa
EDIT
// mirror an input string iteratively.
public static String mirror(String str) {
// return str + reverse(str);
StringBuilder sb = new StringBuilder(str);
return str + sb.reverse().toString();
}
Upvotes: 2