Skip to content

Commit 058f92b

Browse files
committed
Vowel Count
1 parent ffe2fad commit 058f92b

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/easy/VowelCount.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package easy;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
/**
7+
* Have the function VowelCount(str) take the str string
8+
* parameter being passed and return the number of vowels
9+
* the string contains (i.e. "All cows eat grass and moo" would return 8).
10+
* Do not count y as a vowel for this challenge.
11+
*/
12+
public class VowelCount {
13+
14+
/**
15+
* Vowel Count function.
16+
*
17+
* @param str input string
18+
* @return the number of vowels in a string
19+
*/
20+
private static int vowelCount(String str) {
21+
Pattern pattern = Pattern.compile("[aeiou]");
22+
Matcher matcher = pattern.matcher(str);
23+
int i = 0;
24+
int count = 0;
25+
while (matcher.find(i)) {
26+
count++;
27+
i = matcher.start() + 1;
28+
}
29+
return count;
30+
}
31+
32+
/**
33+
* Entry point.
34+
*
35+
* @param args command line arguments
36+
*/
37+
public static void main(String[] args) {
38+
var result1 = vowelCount("I cannot sleep unless I am surrounded by books.");
39+
System.out.println(result1);
40+
var result2 = vowelCount("Life itself is a quotation.");
41+
System.out.println(result2);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)