输入描述:
在一行上输入一个长度为 1≦length(s)≦1000 的字符串。
输出描述:
第一行输出一个整数,代表字符串中英文字母的个数。
第二行输出一个整数,代表字符串中空格的个数。
第三行输出一个整数,代表字符串中数字的个数。
第四行输出一个整数,代表字符串中其它字符的个数。
第二行输出一个整数,代表字符串中空格的个数。
第三行输出一个整数,代表字符串中数字的个数。
第四行输出一个整数,代表字符串中其它字符的个数。
解法一(java):
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
// while (in.hasNextInt()) { // 注意 while 处理多个 case
// int a = in.nextInt();
// int b = in.nextInt();
// System.out.println(a + b);
// }
String str = in.nextLine();
int a = 0;
int b = 0;
int c = 0;
int d = 0;
for (int i = 0 ; i < str.length(); i++) {
char ch = str.charAt(i);
if (Character.isLetter(ch)){
a++;
} else if (ch == 32) {
b++;
} else if (Character.isDigit(ch)) {
c++;
} else {
d++;
}
}
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
}
思路:遍历字符串,统计即可.