So here is the code I found to reverse a string recursively...and I think I understand how it is working, but I just want to know if someone could provide an explanation of it?
public static String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return reverse(str.substring(1)) + str.charAt(0);
}
For instance, when I reverse the word "Hello", I understand that the substring (initially) will be "ello" and then "llo" and then "lo" and "o", but when does the str.charAt(0) get called? Won't the process end after it recursively finds just the "o" and then returns the string?
Thanks!