Question

In: Computer Science

C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1....

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

Solutions

Expert Solution

ALGORITHM

  • Use String Stream to count no of words in a particular string.
  • Run for loop to count all non space char in a string if s[i]!= ' '.
  • To exit program with ctrl+D we can replace while(1) with while(getline(cin,x)) because getline returns EOF.
#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


Related Solutions

PLEASE COMPLETE ALL 3 TABLES FOR THE JOURNAL ENTRIES! MAKE SURE THERE IS THE CORRECT NUMBER...
PLEASE COMPLETE ALL 3 TABLES FOR THE JOURNAL ENTRIES! MAKE SURE THERE IS THE CORRECT NUMBER OF ENTRIES AS PROVIDED IN EACH TABLE. NOT ONE MORE AND NOT ONE LESS OR ELSE I'LL GET WRONG ANSWERS. MAKE SURE THE DATES ARE CORRECT ALSO FOR EACH ENTRY! The following information about the payroll for the week ended December 30 was obtained from the records of Pharrell Co.: Salaries: Sales salaries $402,000 Warehouse salaries 210,000 Office salaries 165,000 $777,000 Deductions: Income tax...
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
Please Use C language to Make a calculator. Make sure calculator is able to take up...
Please Use C language to Make a calculator. Make sure calculator is able to take up to 5 values. Try to make calculator using simple coding As you can. Please create a simple calculator with only +, -,* and divide. It has to be able to enter any numbers (including decimals) and be able to do the following functions: +, -, divide and multiply. Please have the answers, always rounded to two decimal figures. The calculator will also have to...
Instructions: Please make sure everything is correct if not correct and give correct calculation pls. Absorption...
Instructions: Please make sure everything is correct if not correct and give correct calculation pls. Absorption and Variable Costing Income Statements for Two Months and Analysis During the first month of operations ended July 31, Head Gear Inc. manufactured 26,800 hats, of which 25,500 were sold. Operating data for the month are summarized as follows: Sales $183,600 Manufacturing costs: Direct materials $109,880 Direct labor 29,480 Variable manufacturing cost 13,400 Fixed manufacturing cost 10,720 163,480 Selling and administrative expenses: Variable $10,200...
please submit the C code( no third party library). the C code will create output to...
please submit the C code( no third party library). the C code will create output to a file, and iterate in a loop 60 times and each iteration is 1 second, and if any key from your keyboard is pressed will write a 1 in the file, for every second no key is pressed, will write a 0 into the output file.
Use C++ please You will be building a linked list. Make sure to keep track of...
Use C++ please You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. PlaylistNode.h - Class declaration PlaylistNode.cpp - Class definition main.cpp - main() function Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps. Default constructor (1 pt) Parameterized constructor (1 pt) Public member functions InsertAfter() - Mutator (1 pt)...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ *...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ * This program takes a puzzle board and returns true if the * board is solvable and false otherwise * * Example: board 3 6 4 1 3 4 2 5 3 0 * The goal is to reach the 0. You can move the number of spaces of your * current position in either the positive / negative direction * Solution for this game...
PLEASE USE THE ECLIPSE. I want to make a program that shows whether the correct number...
PLEASE USE THE ECLIPSE. I want to make a program that shows whether the correct number is correct or incorrect. Example: how much is 1+1? option 1: 2 option 2: 3 option 3: 4 option 4: 5 option 5: 6 The answer is 1. because the answer is 2 and the option number is 1. Write a Java application that simulates a test. The test contains at least five questions about first three lectures of this course. Each question should...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
use repil.it intro to C-programin be sure Make code very basic and add good comments thank...
use repil.it intro to C-programin be sure Make code very basic and add good comments thank you 1-Write a program that requests 5 integers from the user and stores them in an array. You may do this with either a for loop OR by getting a string from stdin and using sscanf to store formatted input in each spot in the array. 2-Add a function to the program, called get_mean that determines the mean, which is the average of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT