-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString_18.java
More file actions
80 lines (69 loc) · 1.64 KB
/
Copy pathString_18.java
File metadata and controls
80 lines (69 loc) · 1.64 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package HuaweiCoding;
import java.util.Scanner;
public class String_18 {
/*
* 统计出英文字母字符的个数
*/
public static int getEnglishCharCount(String str) {
int counts=0;
for(int i=0;i<str.length();i++) {
char item = str.charAt(i);
if((item>='a' && item<='z') || (item>='A' && item<='Z')) {
counts++;
}
}
return counts;
}
/*
* 统计出空格字符的个数
*/
public static int getBlankCharCount(String str) {
int counts=0;
for(int i=0;i<str.length();i++) {
char item=str.charAt(i);
if(" ".equals(item+"")) {
counts++;
}
}
return counts;
}
/*
* 统计出数字字符的个数
*/
public static int getNumberCharCount(String str) {
int counts=0;
for(int i=0;i<str.length();i++) {
char item=str.charAt(i);
if(item>='0' && item<='9') {
counts++;
}
}
return counts;
}
/*
* 统计出其他字符的个数
*/
public static int getOtherCharCount(String str) {
int counts=0;
for(int i=0;i<str.length();i++) {
char item=str.charAt(i);
if(!(item>='a' && item<='z')&&
!(item>='A' && item<='Z') &&
!(item>='0' && item<='9') &&
!(" ".equals(item+""))) {
counts++;
}
}
return counts;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()) {
String nextLine = sc.nextLine();
System.out.println(getEnglishCharCount(nextLine));
System.out.println(getBlankCharCount(nextLine));
System.out.println(getNumberCharCount(nextLine));
System.out.println(getOtherCharCount(nextLine));
}
}
}