- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountCharacters.java
More file actions
Latest commit
33 lines (26 loc) · 959 Bytes
/
Copy pathCountCharacters.java
File metadata and controls
33 lines (26 loc) · 959 Bytes
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
importjava.util.Scanner;
publicclassCountCharacters {
publicstaticvoidmain(String[] args) {
Scannerscn = newScanner(System.in);
Stringstr = scn.nextLine();
intvowels = 0, consonants = 0, digits = 0, special = 0;
for (charch : str.toCharArray()) {
if(Character.isLetter(ch)){
ch = Character.toLowerCase(ch);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'){
vowels++;
}else{
consonants++;
}
}elseif(Character.isDigit(ch)){
digits++;
}else{
special++;
}
}
System.out.println("Vowels = " + vowels);
System.out.println("Consonants = " + consonants);
System.out.println("Digits = " + digits);
System.out.println("Special Characters = " + special);
}
}