Created
August 18, 2016 22:28
-
-
Save brianteachman/332787f19c5aa0e7853ddcacfff68bcf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Write a C++ program that reads a word from the user into a C string and | |
* reports the number of vowels in the word.Then the program should read a | |
* whole line of text from the user into a string class variable and report | |
* the number of vowels in the whole line. | |
* | |
* For credit, give your instructor a line - by - line walkthrough of your | |
* code explaining how it works.Be prepared to modify your program to alter | |
* the algorithm in some way. | |
* | |
* For extra credit try using a vector of characters and / or highlighting | |
* the vowels with an asterisk on either side or something like that. | |
* | |
* Student: Brian Teachman ([email protected]) | |
* Date: 8/15/2016 | |
* Class: CS& 131: Computer Science I C++ | |
*/ | |
#include "stdafx.h" // msvs specific | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
using namespace std; | |
void runProgram(void); | |
// TODO: input validation needed | |
string getOneWord(void); | |
string getOneLine(void); | |
bool isVowel(char letter); | |
int countVowels(string input); | |
string wrapVowels(string input); | |
int main() | |
{ | |
runProgram(); | |
return 0; | |
} | |
void runProgram(void) | |
{ | |
bool done = false; | |
while (!done) | |
{ | |
string console_input; | |
int menu_option; | |
cout << "Enter an option:" << endl; | |
cout << endl; | |
cout << " 1) Enter one word" << endl; | |
cout << " 2) Enter a sentence" << endl; | |
cout << " 3) Exit Program" << endl; | |
cout << endl; | |
cin >> menu_option; | |
//TODO: validate menu_option | |
switch (menu_option) | |
{ | |
case 1: | |
console_input = getOneWord(); | |
//TODO: validate console_input | |
cout << "Counted " << countVowels(console_input) << " vowels: " | |
<< wrapVowels(console_input) << endl; | |
break; | |
case 2: | |
cin.ignore(); // removes rest of line upto the newline '\n' | |
console_input = getOneLine(); | |
//TODO: validate console_input | |
cout << "Counted " << countVowels(console_input) << " vowels: " | |
<< wrapVowels(console_input) << endl; | |
break; | |
case 3: | |
done = true; | |
break; | |
default: | |
cout << "Please enter a valid selection. "; | |
system("pause"); //msvs specific | |
runProgram(); | |
break; | |
} | |
cout << endl; | |
} | |
} | |
bool isVowel(char letter) | |
{ | |
bool is_found = false; | |
vector<char> vowelList = { 'a', 'e', 'i', 'o', 'u', 'y' }; | |
for (char vowel : vowelList) | |
{ | |
if (letter == vowel || letter == toupper(vowel)) | |
{ | |
is_found = true; | |
} | |
} | |
return is_found; | |
} | |
int countVowels(string inputString) | |
{ | |
int count = 0; | |
for (char letter: inputString) | |
{ | |
if (isVowel(letter)) count++; | |
} | |
return count; | |
} | |
string wrapVowels(string inputString) | |
{ | |
string wrappedString = ""; | |
for (char letter: inputString) | |
{ | |
bool found = isVowel(letter) ? true: false; | |
if (found) | |
{ | |
wrappedString.push_back('['); | |
wrappedString.push_back(letter); | |
wrappedString.push_back(']'); | |
} | |
else | |
{ | |
wrappedString.push_back(letter); | |
} | |
} | |
return wrappedString; | |
} | |
string getOneWord() | |
{ | |
string word; | |
cout << "Please enter one word: "; | |
cin >> word; | |
cout << endl; | |
return word; | |
} | |
string getOneLine() | |
{ | |
string line; | |
cout << "Please enter a line of text (press enter when your done):" << endl | |
<< "---------------------------------------------------------" << endl | |
<< endl; | |
getline(cin, line); | |
return line; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment