-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP37_CountVowelsConsonantsDigits.java
More file actions
41 lines (36 loc) · 1.45 KB
/
Copy pathP37_CountVowelsConsonantsDigits.java
File metadata and controls
41 lines (36 loc) · 1.45 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
package programs;
/**
* ============================================================
* PROGRAM 37: Count Vowels, Consonants, Digits & Special Chars
* ============================================================
* Problem: WAP to count total vowels, consonants, numbers, and
* special characters in a given string.
* ============================================================
*/
public class P37_CountVowelsConsonantsDigits {
public static void analyzeString(String str) {
int vowels = 0, consonants = 0, digits = 0, specials = 0, spaces = 0;
for (char ch : str.toCharArray()) {
if (Character.isDigit(ch)) {
digits++;
} else if (Character.isLetter(ch)) {
char lower = Character.toLowerCase(ch);
if (lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u') {
vowels++;
} else {
consonants++;
}
} else if (Character.isWhitespace(ch)) {
spaces++;
} else {
specials++;
}
}
System.out.println("Input: \"" + str + "\"");
System.out.printf(" Vowels: %d | Consonants: %d | Digits: %d | Spaces: %d | Special: %d%n%n",
vowels, consonants, digits, spaces, specials);
}
public static void main(String[] args) {
analyzeString("Java 21 Mastery @Antigravity #123!");
}
}