package Recursive;
import java.util.Scanner;
public class Exercise18_14 {
public static void main(String[] args) {
System.out.print("Enter a string: ");
String str = new Scanner(System.in).nextLine();
System.out.println("The number of uppercase in string " + str + " is " + count(str));
}
/** 返回字符串中大写字母的个数 */
public static int count(String str) {
return count(str, str.length()-1, 0);
}
/** 求字符串中大写字母的个数辅助方法 */
private static int count(String str, int high, int count) {
count = Character.isUpperCase(str.charAt(high)) ? 1 : 0;
return high == 0 ? count : (count + count(str, high-1, count));
}
}
Java、求字符串中大写字母的个数
最新推荐文章于 2024-03-22 21:19:33 发布