Skip to content

Commit 79dbaec

Browse files
authored
Create VowelsConsonantsEx.java
1 parent 11308cc commit 79dbaec

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

arrays/VowelsConsonantsEx.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.zetcode;
2+
3+
public class VowelsConsonantsEx {
4+
5+
// count the number of vowels and consonants in
6+
// all words of a list
7+
public static void main(String[] args) {
8+
9+
String[] words = {"sun", "forest", "wood", "rocks"};
10+
11+
int vowels = 0;
12+
int consonants=0;
13+
14+
for (int i = 0; i < words.length; i++) {
15+
16+
String word = words[i];
17+
18+
for (int j = 0; j < word.length(); j++) {
19+
20+
char c = word.charAt(j);
21+
22+
if (c == 'a' || c == 'e' || c == 'y' || c == 'i' || c == 'u' || c =='o') {
23+
vowels++;
24+
} else {
25+
consonants++;
26+
}
27+
}
28+
}
29+
30+
System.out.printf("Vowels: %d%n", vowels);
31+
System.out.printf("Consonants: %d%n", consonants);
32+
}
33+
}

0 commit comments

Comments
 (0)