Skip to content

Commit 12e95d0

Browse files
jbhattacjbhattac
authored andcommitted
CountVowelRecursive
1 parent 944c2d4 commit 12e95d0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

CountVowelRecursive.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ds.algo.solutions;
2+
3+
public class CountVowelRecursive {
4+
5+
public static void main(String[] args) {
6+
7+
String str = "hello world";
8+
System.out.println(countVowels(str, str.length() - 1));
9+
}
10+
11+
private static boolean isVowel(char ch) {
12+
return ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' ||
13+
ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
14+
}
15+
16+
private static int countVowels(String str, int pos) {
17+
if (pos == 1)
18+
return isVowel(str.charAt(pos - 1)) ? 1 : 0;
19+
20+
return countVowels(str, pos - 1) + (isVowel(str.charAt(pos - 1)) ? 1 : 0);
21+
}
22+
}

0 commit comments

Comments
 (0)