统计输入的字符串中的各种字符次数

统计输入的字符串中的各种字符次数




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

import java.util.Scanner;

public class demo14{
public static void main(String[] args) {
String str ;
int countNumber = 0;
int countOther = 0;
int countLower = 0;
int countUpper = 0;

Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
str = sc.next();
char[] chs = str.toCharArray();

for (int i = 0; i < chs.length; i++) {
char ch = chs[i];

if(ch >= 'a' && ch <= 'z')
{
countLower++;
}else if(ch >= 'A' && ch <= 'Z' )
{
countUpper++;
}else if(ch >= '0' && ch <= '9' ){
countNumber++;
}else
{
countOther++;
}
}
System.out.println("大写字母个数:" + countUpper);
System.out.println("小写字母个数:" + countLower);
System.out.println("数字个数:" + countNumber);
System.out.println("其他字符个数:" + countOther);
}
}