When I am reversing a String using the recursive method it is giving me the stackOverflow Error.
public class ReverseString {
public static void main(String[] args) {
String str = "Juhi";
System.out.println(recursiveString(str));
}
static String recursiveString(String str) {
return !str.equals("") ? recursiveString(str.substring(1) + str.charAt(0)) : str;
}
}
str.substring(1) + str.charAt(0)so the base case is never true (unless you pass in an emptyStringto begin with) and recursion never stops