3
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 aCt = 0; int eCt = 0; int iCt = 0; int oCt = 0; int uCt = 0; //Function to find the number of vowels. void countVowels(string str) { //To optimize processing time, set the length once. int strLen = str.length(); for (int i=0; i < strLen; i++) { //To optimize processing time, set the character once. char cVal = str[i]; //If the character matches a vowel, increment the total. if (cVal == 'a') { aCt++; } else if (cVal == 'e') { eCt++; } else if (cVal == 'i') { iCt++; } else if (cVal == 'o') { oCt++; } else if (cVal == 'u') { uCt++; } } } 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.

Unit 6 Assignment B(1)

Embed Size (px)

DESCRIPTION

unit 6 assignment B

Citation preview

Page 1: Unit 6 Assignment B(1)

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 aCt = 0; int eCt = 0; int iCt = 0; int oCt = 0; int uCt = 0; //Function to find the number of vowels. void countVowels(string str) { //To optimize processing time, set the length once . int strLen = str.length(); for (int i=0; i < strLen; i++) { //To optimize processing time, set the character once. char cVal = str[i]; //If the character matches a vowel, increment the total. if (cVal == 'a') { aCt++; } else if (cVal == 'e') { eCt++; } else if (cVal == 'i') { iCt++; } else if (cVal == 'o') { oCt++; } else if (cVal == 'u') { uCt++; } } } 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 vo wels. countVowels(tStr); //Print out the total number found for each vowel.

Page 2: Unit 6 Assignment B(1)

cout << "Total 'a' vowels: " << aCt << endl; cout << "Total 'e' vowels: " << eCt << endl; cout << "Total 'i' vowels: " << iCt << endl; cout << "Total 'o' vowels: " << oCt << endl; cout << "Total 'u' vowels: " << uCt << endl; return 0; } =END CODE= OR =START CODE= #include<iostream> #include <string> using namespace std; int aCt = 0; int eCt = 0; int iCt = 0; int oCt = 0; int uCt = 0; //Function to find the number of vowels. void countVowels(string str) { //To optimize processing time, set the length once . int strLen = str.length(); cout << str.length() << 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 vowel, increment the tota l. if (cVal == 'a') { aCt++; } else if (cVal == 'e') { eCt++; } else if (cVal == 'i') { iCt++; } else if (cVal == 'o') { oCt++; } else if (cVal == 'u') { uCt++; } } } int main() { string tStr = ""; //Prompt user to enter a string. cout << "Enter a String or Line of Text You Want t o Check: "; getline(cin, tStr); //Call the function to find the total number of vo wels. countVowels(tStr);

Page 3: Unit 6 Assignment B(1)

//Print out the total number found for each vowel. cout << "Total 'a' vowels: " << aCt << endl; cout << "Total 'e' vowels: " << eCt << endl; cout << "Total 'i' vowels: " << iCt << endl; cout << "Total 'o' vowels: " << oCt << endl; cout << "Total 'u' vowels: " << uCt << endl; cout << "Total vowels: " << (aCt + eCt + iCt + oCt + uCt) << endl; return 0; } =END CODE=