统计字符

统计字符

http://www.nowcoder.com/questionTerminal/539054b4c33b4776bc350155f7abd8f5

四种情况,没什么难点。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String str = in.nextLine();
            int letter = 0;
            int space = 0;
            int num = 0;
            int other = 0;
            for(int i = 0; i < str.length(); i++){
                char c = str.charAt(i);
                if((c >= 'a' && c <= 'z')||(c >= 'A' && c <= 'Z')){
                    letter++;
                }
                else if(c >= '0' && c <= '9'){
                    num++;
                }
                else if(c == ' '){
                    space++;
                }
                else{
                    other++;
                }
            }
            System.out.println(letter);
            System.out.println(space);
            System.out.println(num);
            System.out.println(other);
        }
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-01 12:22
点赞 评论 收藏
分享
这不纯纯作弊了吗😢😢😢
编程界菜鸡:信这个的这辈子有了,这智商你靠啥都没用
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务