面试题3

如果一串字符如”aaaabbc中国1512”要分别统计英文字符的数量,中文字符的数量,和数字字符的数量,假设字符中没有中文字符、英文字符、数字字符之外的其他特殊字符。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.maoge;

public class {
public static void main(String arg[]) {
int digitalCount = 0;
int chineseCount = 0;
int englishCount = 0;

String str = "中国达人秀12399ABC每个deef";

for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= '0' && ch <= '9') {
digitalCount++;
} else if ((ch >= 'a' && ch <= 'z') ||( ch >= 'A' && ch <= 'Z')) {
englishCount++;
} else {
chineseCount++;
}
}

System.out.println("digitalCount=" + digitalCount + "tenglish=" + englishCount + "tchineseCount=" + chineseCount);
}
}

limaodeng

scribble