Write a recursive method that will count the number of vowels in a string. Hint: Each time you make a recursive call, use the String method substring to construct a new string consisting of the second through last characters. The final call will be when the string contains no characters.
What will be an ideal response?
```
public static int countVowels(String s){
int result;
if(s.length() == 0)
result = 0;
else {
if(isVowel(s.charAt(0)))
result = 1 + countVowels(s.substring(1));
else
result = countVowels(s.substring(1));
}
return result;
}
public static boolean isVowel(char c){
return c=='a' || c=='e' || c=='i' || c=='o' || c=='u'
|| c=='A' || c=='E' || c=='I' || c=='O' || c=='U';
}
```
This code is in Methods.java.
You might also like to view...
A power strip and surge suppressor offer the same protection and features that a UPS does
Indicate whether the statement is true or false
Being effective means meeting the actual goals of a project rather than just doing the project tasks quickly and cheaply.
Answer the following statement true (T) or false (F)
Which of the following is NOT a sparkline option?
A. Line B. Column C. Pie D. Win/Loss
When assigned to a cell, the _____ function returns a number that corresponds to the system date and time beginning with December 31, 1899.
A. SYSDATE B. NOW C. DATETIME D. CURRENT