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...
Bookmark names:
A) are limited to eight letters. B) are given the Heading 1 format. C) cannot contain spaces. D) must be the same as the document headings.
The ________ is a nonprofit group of computer specialists that sets standards for HTML
Fill in the blank(s) with correct word