-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVowelCounter.java
More file actions
50 lines (44 loc) · 1.42 KB
/
VowelCounter.java
File metadata and controls
50 lines (44 loc) · 1.42 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
42
43
44
45
46
47
48
49
50
package module4.recursion.vowelcounter;
/**
* VowelCounter
*/
public class VowelCounter {
public static final char[] VOWELS = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
/**
* Uses recursive methods to count the number of vowels (a-e-i-o-u) in a word or phrase
* @param str The string to count
* @return The number of vowels to count
*/
public static int vowelCount(String str) {
if (str.length() == 0) {
return 0;
}
int count = vowelCount(str.substring(1));
if (str.substring(0, 1).matches("[aeiouAEIOU]" /* */)) {
return count + 1;
}
return count;
}
public static void main(String[] args) {
System.out.println();
System.out.println("=== RECURSIVE VOWEL COUNTER DEMO ===");
final String[] demoStrings = {
"AIUEOaiueo", "abab", "queueing", "buffalo", "4BS0L6T3LY L33T", "hachi machi", "hOnK hOnK mOtHeRfAtHeR", "bienvenue", "power bottoms"
};
for (String s : demoStrings) {
System.out.printf("vowelCount(\"%s\") => %d\n", s, vowelCount(s));
}
}
}
/**
Console Output:
=== RECURSIVE VOWEL COUNTER DEMO ===
vowelCount("AIUEOaiueo") => 10
vowelCount("abab") => 2
vowelCount("queueing") => 5
vowelCount("buffalo") => 3
vowelCount("hachi machi") => 4
vowelCount("hOnK hOnK mOtHeRfAtHeR") => 6
vowelCount("bienvenue") => 5
vowelCount("power bottoms") => 4
*/