I've having trouble reversing a string using recursion - I'm pretty new at this concept.
I'm testing my code with the word "hello". It is only printing out 'h' at the end and not the entire string I was trying to build up during the recursion. I know using strings to add is slow but I haven't thought about making it faster right now since it is not working.
public class MyClass
{
public static void main(String[] args) {
System.out.println(reversedString("hello", 0, ""));
}
public static String reversedString(String str, int index, String sb) {
if(index == str.length()) {
return null;
}
reversedString(str, index + 1, sb);
sb = sb + str.charAt(index);
return sb;
}
}