i need to write a method that takes in a string as a parameter and prints the word in mirrored form. for example, “hello” should return “helloolleh”. I have to use recursion and cannot use for loops. Here is my code so far:
public static String printMirrored(String str)
{
if(str == null || str.equals(""))
{
return str;
}
else
{
return str + printMirrored(str.substring(1)) + str.charAt(0);
}
}
My output is “helloellollolooolleh” which obviously has some extra things in there. Any pointers would be greatly appreciated!
Try using
str.charAt(0)at the beginning instead ofstr.