This programs counts the vowels present in the given string using the recurions.
Program to count the vowels using Recurions:
public static void main(String[] args){
String str = "NgDevelOper.com";
System.out.println(countVowels(str, str.length()));
}
private static int countVowels(String str, int length) {
if(length ==1){
if (isVowel(str.charAt(length-1))) {
return 1;
}else {
return 0;
}
}
return (countVowels(str, length-1)) + (isVowel(str.charAt(length-1)) ? 1 : 0);
}
private static boolean isVowel(char charAt) {
if(charAt == 'a' || charAt == 'e' || charAt == 'i' || charAt == 'o' || charAt == 'u' || charAt == 'A' || charAt == 'E' || charAt == 'I' || charAt == 'O' || charAt == 'U')
return true;
return false;
}
Output:
Input: NgDevelOper.com
5