Unit 6 Assignment B
Programming Exercise #20 (Chapter 6)
Write a function that takes as a parameter a string and returns the number of times each
lowercase vowels appears in it (a, e, i, o, u). The function prototype should look like:
void countVowels(string str, int& aCt, int& eCt, int& iCt, int& oCt, int& uCt);
Also, write a main program that tests your function by allowing the user to enter in a
string and is passed to the function.
=START CODE=
#include <iostream>
#include <string>
using namespace std;
int
int
int
int
int
aCt
eCt
iCt
oCt
uCt
=
=
=
=
=
0;
0;
0;
0;
0;
//Function to find the number of vowels.
void countVowels(string str) {
//To optimize processing time, set the length once.
int strLen = [Link]();
for (int i=0; i < strLen; i++) {
//To optimize processing time, set the character once.
char cVal = str[i];
//If the character
if (cVal == 'a') {
aCt++;
} else if (cVal ==
eCt++;
} else if (cVal ==
iCt++;
} else if (cVal ==
oCt++;
} else if (cVal ==
uCt++;
}
matches a vowel, increment the total.
'e') {
'i') {
'o') {
'u') {
}
}
int main() {
string tStr = "";
//Prompt user to enter a string.
cout << "Enter a String You Want to Check: ";
cin >> tStr;
//Call the function to find the total number of vowels.
countVowels(tStr);
//Print out the total number found for each vowel.
cout
cout
cout
cout
cout
<<
<<
<<
<<
<<
"Total
"Total
"Total
"Total
"Total
'a'
'e'
'i'
'o'
'u'
vowels:
vowels:
vowels:
vowels:
vowels:
"
"
"
"
"
<<
<<
<<
<<
<<
aCt
eCt
iCt
oCt
uCt
<<
<<
<<
<<
<<
endl;
endl;
endl;
endl;
endl;
return 0;
}
=END CODE=
OR
=START CODE=
#include<iostream>
#include <string>
using namespace std;
int
int
int
int
int
aCt
eCt
iCt
oCt
uCt
=
=
=
=
=
0;
0;
0;
0;
0;
//Function to find the number of vowels.
void countVowels(string str) {
//To optimize processing time, set the length once.
int strLen = [Link]();
cout << [Link]() << endl;
for (int i=0; i < strLen; i++) {
//To optimize processing time, set the character once.
char cVal = str[i];
//If the char matches a
if (cVal == 'a') {
aCt++;
} else if (cVal == 'e')
eCt++;
} else if (cVal == 'i')
iCt++;
} else if (cVal == 'o')
oCt++;
} else if (cVal == 'u')
uCt++;
}
vowel, increment the total.
{
{
{
{
}
}
int main() {
string tStr = "";
//Prompt user to enter a string.
cout << "Enter a String or Line of Text You Want to Check: ";
getline(cin, tStr);
//Call the function to find the total number of vowels.
countVowels(tStr);
//Print
cout <<
cout <<
cout <<
cout <<
cout <<
cout <<
out the total number found for
"Total 'a' vowels: " << aCt <<
"Total 'e' vowels: " << eCt <<
"Total 'i' vowels: " << iCt <<
"Total 'o' vowels: " << oCt <<
"Total 'u' vowels: " << uCt <<
"Total vowels: " << (aCt + eCt
endl;
return 0;
}
=END CODE=
each vowel.
endl;
endl;
endl;
endl;
endl;
+ iCt + oCt + uCt) <<