forked from sanketpatil02/Code-Overflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount of vowels & consonants in string
28 lines (24 loc) · 1.12 KB
/
Count of vowels & consonants in string
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
public class CountVowelConsonant {
public static void main(String[] args) {
//Counter variable to store the count of vowels and consonant
int vCount = 0, cCount = 0;
//Declare a string
String str = "This is a really simple sentence";
//Converting entire string to lower case to reduce the comparisons
str = str.toLowerCase();
for(int i = 0; i < str.length(); i++) {
//Checks whether a character is a vowel
if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') {
//Increments the vowel counter
vCount++;
}
//Checks whether a character is a consonant
else if(str.charAt(i) >= 'a' && str.charAt(i)<='z') {
//Increments the consonant counter
cCount++;
}
}
System.out.println("Number of vowels: " + vCount);
System.out.println("Number of consonants: " + cCount);
}
}