您的位置:首页 > 其它

【计蒜客系列】挑战难题374:统计个数(课后)

2015-09-24 09:15 423 查看
题目来源:计蒜客

输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。

一行字符

统计值

样例1

输入:

aklsjflj123 sadf918u324 asdf91u32oasdf/.';123

输出:

23 16 2 4

import java.util.*;

public class Main {

public static void main(String args[]) throws Exception {
Scanner scanner = new Scanner(System.in);

String str = scanner.nextLine();

if(str.length() != 0){
countNum(str);
}else{
System.out.println("error");
}

}

public static void countNum(String src){
char[] cha = src.toCharArray();

int eng = 0;
int blank = 0;
int num = 0;
int other = 0;

for(int i = 0;i < cha.length;i++){
if((cha[i] >= 'a' && cha[i] <= 'z') || (cha[i] >= 'A' && cha[i] <= 'Z')){
eng++;
}else if(cha[i] == ' '){
blank++;
}else if(cha[i] >= '0' && cha[i] <= '9'){
num++;
}else{
other++;
}
}
System.out.println(eng + " " + num + " " + blank + " " + other);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: