Question

In: Computer Science

C++ CODE Change your code from Part A to now present a menu to the user...

C++ CODE

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.

Example 1:

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
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.

Solutions

Expert Solution

SOLUTION-
I have solve the problem in C++ code with comments and screenshot for easy understanding :)

CODE-

//c++ code
#include <iostream>
using namespace std;
// declarations for function asked in assignment.
int NumWords(string &line);
int NumNonWSCharacters(string &line);
char PrintMenu();
void CharReplace(string &line, char rep,char repw);
//main function
//its takes input string from user and call PrintMenu()
// based of choice while loop will run until user enters 'q' or Ctrl-d
//choice is 'w' it call the NumWords(&string) and returns count of letters in it.
//choice is 'c' it call the NumNonWSCharacters(&string) and returns the letter without space
//choice is 'r' it call CharReplace(&string,char,char)
//main function
int main()
{
string line;
char choice,rep,repw;
cout<<"Enter a line of text: ";
getline(cin, line); //gets input
cout<<"You Entered: "<<line<<endl;
choice=PrintMenu(); //user choice
while(choice!='q'|| getline(std::cin, line)){
if(choice=='w')
{
cout<<"Number of words: "<<NumWords(line)<<endl;
}
else if(choice=='c')
{
cout<<"Number of non-whitespace characters: "<<NumNonWSCharacters(line)<<endl;
}
else if(choice=='r')
{
cout<<"Enter a character to find: ";
cin>>rep;
cout<<"Enter a character to replace: ";
cin>>repw;
CharReplace(line,rep,repw);
cout<<"New String: "<<line<<endl;
}
choice=PrintMenu();
}
return 0;
}
//PrintMenu to show options and return choice based on input.
char PrintMenu(){
char choice;
cout<<"\nOptions"<<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;
cout<<"Choose an option: ";
cin>>choice;
return choice;
}

//NumWords(&string) it loop the whole string untill '\0' and return count.
int NumWords(string& line){
int count=0;
for(int i=0;line[i]!='\0';i++){
count++;
}
return count;
}
//NumWords(&string) it loop the whole string untill '\0' and if char is space it skips and return count.
int NumNonWSCharacters(string& line){
int count=0;
for(int i=0;line[i]!='\0';i++){
  
if(!isspace(line[i]))
count++;
}
return count;
}
// it replace the rep with repw while iterating each char
void CharReplace(string& line, char rep,char repw){
for(int i=0;line[i]!='\0';i++){
  
if(line[i]==rep){
line[i]=repw;
}

}
}


SCREENSHOT-


IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

Write a C++ code to print to the user a simple menu of a fast food...
Write a C++ code to print to the user a simple menu of a fast food restaurant. You should allow the user to select his/her preferred burgers and/or drinks and you should display the final bill to the user for payment. The user should be able to select more than one item. You should use the switch statement.
print a menu so that the user can then order from that menu. The user will...
print a menu so that the user can then order from that menu. The user will enter each item they want until they are done with the order. At the end you will print the items the user ordered with their price and the total. Note: You will be storing your menu items (pizza, burger, hotdog, salad) and your prices (10, 7, 4, 8) in separate lists. Menu 0) pizza $10 1) burger $7 2) hotdog $4 3) salad $8...
Build and present a menu to a user like the following and enclose it into a...
Build and present a menu to a user like the following and enclose it into a loop that ends when the Quit option is chosen. Scan the user's selection into an integer variable. 1. Enter user name. 2. Enter test scores. 3. Display average. 4. Display summary. 5. Quit. Selection: If the user selects Option #1, scan the user's name, and store it to a char array. Assume the user's name is under 20 characters. If the user selects Option...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
This is in Python: Alter your code for previous Do It Now problem, with the change...
This is in Python: Alter your code for previous Do It Now problem, with the change that the row should only be printed by the second number that the user enters. For example, the user enters two numbers of 5 and 7 for the two input lines in the code, and the following will be printed: 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 =...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code should print the numbers in descending order.
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Important! Consider which control structures will work best for which aspect of the assignment. For example, which would be the best to use for a menu?...
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Your code must contain at least one of all of the following control types: nested for() loops a while() or a do-while() loop a switch() statement...
A. Write a C++ with a menu (using switch) that asks the user to select one...
A. Write a C++ with a menu (using switch) that asks the user to select one of the following choices to the user: 1. Options ‘S’ or ‘s’ 2. Option ‘T’ or ‘t 3. Options ‘P’ or ‘p’ 4. print in the Matrix format B. When 1 is selected, prompt the user to enter the starting value st (int value). Use a single FOR loop to count numbers from 1 to st. When the loop is finished, find the average...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT