Recursive characterization of the operation of counting the occurrences of the
character *c* in the string *s*:

- if
*s*is the empty string`""`, then return 0; - otherwise, if the first character of
*s*is equal to*c*, then return 1 plus the number of occurrences of*c*in the string equal to*s*without the first character; - otherwise (i.e., if the first character of
*s*is different from*c*), return the number of occurrences of*c*in the string equal to*s*without the first character.

Implementation:

public static int countChars(String s, char c) { if (s.length() == 0) return 0; else if (s.charAt(0) == c) return 1 + countChars(s.substring(1), c); else return countChars(s.substring(1), c); }