In: Computer Science
C++ CODE
PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT
1. You must call all of the defined functions above in your code. You may not change function names, parameters, or return types.
2. You must use a switch statement to check the user's menu option choice.
3. You may create additional functions in addition to the required functions listed above if you would like.
4. If user provides option which is not a choice in the menu, simply print the menu and prompt for an option again.
Change your code from Part A to now present a menu to the user asking them for an operation to perform on the text that was input. You must include the following functions in your code exactly as provided. Additionally, you must have function prototypes and place them above your main function, and the function definitions should be placed below the main function. Your program should gracefully exit if the user inputs the character q or sends the EOF signal by pressing Ctrl-D.
1. Write a function with the following prototype: int
NumWords(const string&);
This function will take a constant string reference as a parameter
and return the number of words in the string.
2. Write a function with the following prototype: int
NumNonWSCharacters(const string&);
This function will take a constant string reference as a parameter
and return the number of characters contained in the string after
all whitespace characters have been removed.
3. Write a function with the following prototype: void
CharReplace(string&, char, char);
This function takes three parameters: a string reference to modify,
a character to find in the string, and a character to replace each
found character with. Replace the corresponding characters in the
string reference parameter. The function should not return
anything. You may not use string::replace.
4. Write a function with the following prototype: char
PrintMenu();
This function will output the menu displayed in the example output
below to the console and read and return a single character of
input from the user.
Enter a line of text: Help, help, I need some help! You entered: Help, help, I need some help! Options w - Number of words c - Number of non-whitespace characters r - Replace a character q - Quit Choose an option: w Number of words: 6 Options w - Number of words c - Number of non-whitespace characters r - Replace a character q - Quit Choose an option: c Number of non-whitespace characters: 24 Options w - Number of words c - Number of non-whitespace characters r - Replace a character q - Quit Choose an option: r Enter a character to find: h Enter a character to replace: w New string: Help, welp, I need some welp! Options w - Number of words c - Number of non-whitespace characters r - Replace a character q - Quit Choose an option: r Enter a character to find: o Enter a character to replace: a New string: Help, welp, I need same welp! Options w - Number of words c - Number of non-whitespace characters r - Replace a character q - Quit Choose an option: j Options w - Number of words c - Number of non-whitespace characters r - Replace a character q - Quit Choose an option: q
ALGORITHM
#include<bits/stdc++.h>
using namespace std;
int NumWords(const string&);
int NumNonWSCharacters(const string&);
void CharReplace(string&, char, char);
char PrintMenu();
int main()
{
PrintMenu();
}
int NumWords(const string &s )
{
stringstream str(s);
string word;
int count = 0;
while (str >> word)
count++;
return count;
}
int NumNonWSCharacters(const string &s)
{
int cnt=0;
for(int i=0;i<s.size();i++)
{
if(s[i]!=' ')
cnt++;
}
return cnt;
}
void CharReplace(string &s, char x, char y)
{
for(int i=0;i<s.size();i++)
{
if(s[i]==x)
s[i]=y;
}
}
char PrintMenu()
{
cout<<"Enter String"<<endl;
string s;
getline(cin,s);
// Menu
while(1){
cout<<"Options"<<endl;
cout<<"w - Number of words"<<endl;
cout<<"c - Number of non-whitespace characters"<<endl;
cout<<"r - Replace a character"<<endl;
cout<<"q - Quit"<<endl;
char x,a,b;
cin>>x;
switch(x)
{
case 'w':
cout<<NumWords(s)<<endl;
break;
case 'c':
cout<<NumNonWSCharacters(s)<<endl;
break;
case 'r':
cin>>a>>b; // replace all 'a' with 'b';
CharReplace(s,a,b);
cout<<s<<endl;
break;
case 'q':
exit(1);
break;
}
}
}
OUTPUT