题解 | #统计字符#

统计字符

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

import java.util.Scanner;
public class Main {
    /**
    * 输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。
    */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str = sc.nextLine();
            int l = 0, d = 0, b = 0, o = 0;
            for (int i = 0, len = str.length(); i < len; i++) {
                char c = str.charAt(i);
                if (Character.isLetter(c)) {
                    l++;
                } else if (Character.isDigit(c)) {
                    d++;
                } else if (' ' == c) {
                    b++;
                } else {
                    o++;
                }
            }
            System.out.println(l + "\n" + b + "\n" + d + "\n" + o);
        }
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务