Recursive Character Counting
What is printed when the following program is executed?
public class Main {
public static int countChar(String s, char c) {
if(s.length() == 0)
return 0;
int count = (s.charAt(0) == c) ? 1 : 0;
return count + countChar(s.substring(1), c);
}
public static void main(String[] args) {
System.out.println(countChar("banana", 'a'));
}
}
A
0
B
3
C
2
D
4
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE