0
点赞
收藏
分享

微信扫一扫

笔试算法《统计字符》


描述
输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。

本题包含多组输入。

输入描述:
输入一行字符串,可以有空格

输出描述:
统计其中英文字符,空格字符,数字字符,其他字符的个数

示例1
输入:

1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\/;p0-=\][

输出
26
3
10
12

代码

public class Huawei统计字符 {

public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str;
while ((str = reader.readLine()) != null) {
char[] chars = str.toCharArray();
int charNum = 0, space = 0, num = 0, others = 0;
for (int i = 0; i < chars.length; i++) {
if (chars[i]>='0'&&chars[i]<='9'){
num++;
}else if (chars[i]>='a'&&chars[i]<='z'||chars[i]>='A'&&chars[i]<='Z'){
charNum++;
}else if (chars[i]==' '){
space++;
}else{
others++;
}
}
System.out.println(charNum);
System.out.println(space);
System.out.println(num);
System.out.println(others);

}
}
}


举报

相关推荐

0 条评论