<h2>
Answer:</h2>
//Define the method as noOfVowels
//Let it receive the string to be checked as argument
public static int noOfVowels(String string){
//Check the length of the string
//If it is less than 1, then it is an empty string
//Hence return 0
if(string.length() < 1){
return 0;
}
//Else,
else{
//get the character at the first position in the string
//and convert it to a lower case
char x = string.toLowerCase().charAt(0);
//Check if the character is a vowel
if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u'){
//if it is a vowel, add 1 to a recall of the method.
//But this time, call the method with
//the string excluding the first character.
//i.e the substring of the string starting at index 1 rather than 0
return 1 + noOfVowels(string.substring(1));
}
else {
//If it is not a vowel, just recall the method with
//the string excluding the first character
//i.e the substring of the string starting at index 1 rather than 0
return noOfVowels(string.substring(1));
}
}
}
<h2>
Explanation:</h2><h2>
</h2>
The code has been written in Java and it contains comments explaining every part of the code. Please go through the comments carefully.
The actual lines of code have been written in bold face to distinguish them from comments.
The code is re-written without comments as follows;
public static int noOfVowels(String string){
if(string.length() < 1){
return 0;
}
else{
char x = string.toLowerCase().charAt(0);
if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u'){
return 1 + noOfVowels(string.substring(1));
}
else{
return noOfVowels(string.substring(1));
}
}
}