Solution Review 1: Let's Find the Total Number of Vowels
This lesson provides a detailed review of the solution to the challenge in the previous lesson.
Solution: How many vowels were there?
Press + to interact
#include <iostream>#include <string>using namespace std;int totalVowels(string text, int len, int index){//Write your code hereint count=0;if (len==0){return 0;}char single=toupper(text[index]);if(single=='A' || single=='E' || single=='I' || single=='O' || single=='U'){count++;}return count + totalVowels(text,len-1,index+1);}//Function to test your codeint main(){cout<<"The string is: Hello World"<<endl;cout<<"The total number of vowels in this string are: "<<totalVowels("Hello World",10,0)<<endl;cout<<"The string is: STR"<<endl;cout<<"The total number of vowels in this string are: "<<totalVowels("STR",3,0)<<endl;cout<<"The string is: AEIOUaeiouSs"<<endl;cout<<"The total number of vowels in this string are: "<<totalVowels("AEIOUaeiouSs",12,0)<<endl;}
Understanding the Code
Every recursive code has two functions; the recursive function and the main function.
Driver Function
The recursive function is called within the driver function so lets first look at what it does, from line 20 to 31.
- There are three calls made to the recursive function and for each call, it prints the given string and the total number