I can't use other methods or add parameters and can only call the method recursively. I can't really understand how to approach this problem. I am struggling a bit wrapping my mind around recursion so bare with me.
The substring will be enclosed between the characters first and last. Can someone please guide me? Aside from my base case I know what I have down is not right..I feel like I don't completely understand what is going on at each step.
public static String theSub(String str, char first, char last) {
if (str.length() == 0 ) {
return str;
}
else if (str.charAt(0)==first) {
return str.substring(1, str.length() - 1);
}
//infinite recursion..
return theSub(str,first,last);
}
For example, if the string is "f(f(f(x)))", and you want to get x.
Essentially, while both first and last are present, get the substring and make a recursive call.