Hi!
I'm trying to write a method that finds the index of a specified occurrence of a character in a given string using recursion. I've managed to count the number of times a character occurs in the string, but can't figure out how to find the index of a particular character using recursion.
Any help would be greatly appreciated. Here is my code so far:
public static int nthIndexOf (int n, char ch, String str) {
if (str == null || str.equals("") || n <= 0) {
return -1;
}
int restOfString = nthIndexOf(n,ch,str.substring(1));
if (ch == str.charAt(0)) {
restOfString++;
}
/* if (n != (restOfString + 1)) {
restOfString = nthIndexOf(n,ch,str.substring(1));
}*/
return restOfString;
}