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...
C Code Edit this code to make it output all unique letters IN LOWERCASE of an...
C Code Edit this code to make it output all unique letters IN LOWERCASE of an input file. For example, with this input: Raspberry Grapefruit Straw berry raspBERRY Blue$$berry apple Pine_apple raspberry The output should be: raspberry grapefruit straw berry blue berry apple pine NOTE: Words with different capitlization, like raspberry and raspBERRY count as 1 unique word, so raspberry should only be seen once in the output. #include <stdio.h> #include <stdlib.h> int main() {     //Pointer to access the...
C++: Write correct C++ code for a nested if-else if-else structure that will output a message...
C++: Write correct C++ code for a nested if-else if-else structure that will output a message based on the logic below. A buyer can pay immediately or be billed. If paid immediately, then display "a 5% discount is applied." If billed, then display "a 2% discount is applied" if it is paid in 30 days. If between 30 and 60 days, display "there is no discount." If over 60 days, then display "a 3% surcharge is added to the bill."...
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.
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...
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)...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT