My homework is to make a recursive method to count the appearances of a given letter in a given string. Here is my code so far:
import java.util.Scanner;
public class Exercise18_10 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.next();
System.out.print("Enter a character: ");
String letter = sc.next();
char a = letter.charAt(0);
System.out.println("The count of " + a + " is: " + count(str, a));
}
public static int count(String str, char a) {
int count = str.indexOf(a);
return count;
}
}
In count, I use indexOf to find the first occurrence of the desired letter, but I'm not sure what to do after that.
indexOf(...)does?