// 输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数。
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int english = 0, shuzi = 0, kongge = 0, qita = 0;
for (int i = 0; i < str.length(); i++) {
char out = str.charAt(i);
if (out >= 'a' && out <= 'z' || out >= 'A' && out <= 'Z') {
english++;
} else if (out >= '0' && out <= '9') {
shuzi++;
} else if (out == ' ') {
kongge++;
} else {
qita++;
}
}
System.out.print(english+" "+shuzi+" "+kongge+" "+qita);
字符串分类统计
最新推荐文章于 2022-01-22 16:50:14 发布