In: Computer Science
-> > Use recursion to write a C++ function for determining if a strings has more vowels than consonants. Please include the pseudo code so that I can understand better with simple English as much as possible.
Program is easy to understand. Also output is attached as image.
#include<iostream>
using namespace std;
// Function to check a character for Vowel
bool isVowel(char ch)
{
// To handle lower case
ch = toupper(ch);
return (ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U');
}
//Function to count total number of vowels
int countVovels(string s, int n)
{
if (n == 1){return isVowel(s[n-1]);}
return countVovels(s, n-1) + isVowel(s[n-1]);
}
bool isConsonant(char ch)
{
ch = toupper(ch);
return !(ch == 'A' || ch == 'E' ||
ch == 'I' || ch == 'O' ||
ch == 'U') && ch >= 65 && ch <= 90;
}
int totalConsonants(string str, int n)
{
if (n == 1)
return isConsonant(str[0]);
return totalConsonants(str, n - 1) +
isConsonant(str[n-1]);
}
int main()
{
string s = "pqwerdtub";
int v=countVovels(s, s.length());
int c=totalConsonants(s, s.length());
cout <<"Total No. of Vowels-"<<v<< endl;
cout <<"Total No. of Consonants-"<<c<< endl;
if(v>c){cout<<"No. of Vowels are more than the No. of Consonants."<<endl;}
else{cout<<"No. of Vowels are not more than the No. of Consonants."<<endl;}
return 0;
}