-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingVowelsAndConsonants.java
More file actions
37 lines (29 loc) · 1004 Bytes
/
CountingVowelsAndConsonants.java
File metadata and controls
37 lines (29 loc) · 1004 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
34
35
36
37
package dev;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class CountingVowelsAndConsonants {
public static void main(String[] args) {
final Set<Character> allVowels = new HashSet(Arrays.asList('a', 'e', 'i', 'o', 'u'));
long vowels = 0;
long consonants = 0;
String str = "Hasan";
str = str.toLowerCase();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (allVowels.contains(ch)) {
vowels++;
} else if ((ch >= 'a' && ch <= 'z')) {
consonants++;
}
}
vowels = str.chars()
.filter(c -> allVowels.contains((char) c))
.count();
consonants = str.chars()
.filter(c -> !allVowels.contains((char) c))
.filter(ch -> (ch >= 'a' && ch <= 'z'))
.count();
System.out.println(vowels + " " + consonants);
}
}