题解 | #统计字符#

统计字符

https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String a = in.nextLine();
            v(a);
        }
    }

    private static void v(String str) {
        char[] chars = str.toCharArray();
        int alpha = 0;
        int space = 0;
        int num = 0;
        for (char c:chars) {
            if (c >= 'a' && c <= 'z') alpha++;
            if (c >= 'A' && c <= 'Z') alpha++;
            if (c == ' ') space++;
            
            if ("0123456789".indexOf(c) >= 0) num++;
        }
        System.out.println(alpha);
        System.out.println(space);
        System.out.println(num);
        int o = str.length() - alpha - space - num;
        System.out.println(o);
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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