Problem S: C语言-字符统计

Description

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

Input

一行字符

Output

统计值

Sample Input

aklsjflj123 sadf918u324 asdf91u32oasdf/.';123

Sample Output

23 16 2 4

import java.util.Scanner;
 
public class Main {
     public static void main(String[] args) {
         Scanner cin = new Scanner(System.in);
         String str = cin.nextLine();
         int a = 0, b = 0, m = 0, n = 0;
         for (int i = 0; i < str.length(); i++) {
             char c = str.charAt(i);
             if (Character.isLetter(c)) {
                 a++;
             } else if (Character.isWhitespace(c)) {
                 b++;
             } else if (Character.isDigit(c)) {
                 m++;
             } else
                 n++;
         }
         System.out.println(a + " " + m + " " + b + " " + n);
     }
}

你可能感兴趣的:(Problem S: C语言-字符统计)