In: Computer Science
Program Zeus: Complete the Count Vowels program (Count Vowels.cpp template provided at the end) to include two user defined functions (which you will write!). Code lenguage is C++
bool isVowel (char c) // returns true if c is a vowel and false otherwise
int countVowels (string s) // returns the number of vowels in s.
You can access each character of s by using s.at(i) where i ranges from 0 to s.length()-1.
countVowels () should call isVowel (s.at(i)) to check if character i is a vowel.
Run the program once with the following inputs. For full credit, your output should be the same as the output below.
Please enter a word or type "quit" to exit: apple
apple has 2 vowels.
Please enter a word or type "quit" to exit: twang
twang has 1 vowel.
Please enter a word or type "quit" to exit: xyz
xyz has 0 vowels.
Please enter a word or type "quit" to exit: aeiou
aeiou has 5 vowels.
Please enter a word or type "quit" to exit: antidisestablishmentarianism
antidisestablishmentarianism has 11 vowels.
Please enter a word or type "quit" to exit: quit
Goodbye!
Template for Program:
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
bool isVowel(char);
int countVowels(string); .
int main ( )
{
string word;
int numVowels;cout << "Please enter a word or type \"quit\" to exit: ";
cin >> word;
while ()
{
cout << "Please enter a word or type \"quit\" to exit: ";
cin >> word;
}
cout << "Goodbye!" << endl;
return 0;
}
// This function returns the number of vowels in s.
// This function will call isVowel.
int countVowels(string s)
{
}
// This function returns true if c is a vowel and false otherwise
bool isVowel(char c)
{
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
bool isVowel(char);
int countVowels(string);
int main()
{
string word;
int numVowels;
cout << "Please enter a word or type \"quit\" to exit: ";
cin >> word;
//looping as long as word is not "quit"
while ( word!="quit") {
//counting vowels
numVowels=countVowels(word);
//displaying results
cout<<word<<" has "<<numVowels;
//printing "vowel." if count is 1, "vowels." if count is not 1
cout<<(numVowels==1?" vowel.":" vowels.")<<endl;
//asking, reading next word
cout << "Please enter a word or type \"quit\" to exit: ";
cin >> word;
}
cout << "Goodbye!" << endl;
return 0;
}
// This function returns the number of vowels in s.
// This function will call isVowel.
int countVowels(string s)
{
//initializing count to 0
int c=0;
//looping through s
for(int i=0;i<s.length();i++){
//checking if char at i is vowel
if(isVowel(s.at(i))){
//updating count
c++;
}
}
return c;
}
// This function returns true if c is a vowel and false otherwise
bool isVowel(char c)
{
//returning true if c is a/e/i/o/u or A/E/I/O/U
return c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || c=='A' || c=='E' || c=='I' || c=='O' || c=='U';
}
/*OUTPUT*/
Please enter a word or type "quit" to exit: apple
apple has 2 vowels.
Please enter a word or type "quit" to exit: twang
twang has 1 vowel.
Please enter a word or type "quit" to exit: xyz
xyz has 0 vowels.
Please enter a word or type "quit" to exit: aeiou
aeiou has 5 vowels.
Please enter a word or type "quit" to exit: antidisestablishmentarianism
antidisestablishmentarianism has 11 vowels.
Please enter a word or type "quit" to exit: quit
Goodbye!