题目:输入一个字符串,分别统计出英文字母、数字、空格和其他字符的数量。
public class CharStatDemo {
public void analyseNum(String content) {
int eng_letters_count = 0;
int space_count = 0;
int figure_count = 0;
int other_count = 0;
for (int i = 0; i < content.length(); i++) {
char c = content.charAt(i);
if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z') {
eng_letters_count++;
} else if (c == ' ') {
space_count++;
} else if (c >= '0' && c <= '9') {
figure_count++;
} else {
other_count++;
}
}
StringBuffer sb = new StringBuffer();
sb.append("英文字母个数为:").append(eng_letters_count).append(";空格字符个数为:").append(space_count).append(";数字个数为:")
.append(figure_count).append(";其他字符个数为:").append(other_count);
System.out.println(sb.toString());
}
public static void main(String[] args) {
CharStatDemo demo = new CharStatDemo();
demo.analyseNum("abcdefg12654 fr54wfe4 &*^*&%^(hfkjas 154101");
}
}