package Recursive;
import java.util.Scanner;
public class Exercise18_15 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = input.nextLine();
System.out.print("Enter a char: ");
char a = input.next().charAt(0);
System.out.println("The number of char " + a + " in string " + str + " is " + count(str, a));
}
/** 返回字符串中某个指定字符出现的次数 */
public static int count(String str, char a) {
return count(str, a, str.length()-1);
}
/** 返回字符串中某个指定字符出现的次数辅助方法 */
public static int count(String str, char a, int high) {
int c = (str.charAt(high) == a) ? 1 : 0;
return 0 == high ? c : c + count(str, a, high - 1);
}
}