Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is C++ (1) Prompt the user to enter a string of their choosing. Store the t

ID: 3862862 • Letter: T

Question

This is C++

(1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt)

Ex:


(2) Implement a PrintMenu() function, which has a string as a parameter, outputs a menu of user options for analyzing/editing the string, and returns the user's entered menu option. Each option is represented by a single character.

If an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call PrintMenu() in the main() function. Continue to call PrintMenu() until the user enters q to Quit. (3 pts)

Ex:


(3) Implement the GetNumOfNonWSCharacters() function. GetNumOfNonWSCharacters() has a constant string as a parameter and returns the number of characters in the string, excluding all whitespace. Call GetNumOfNonWSCharacters() in the PrintMenu() function. (4 pts)

Ex:


(4) Implement the GetNumOfWords() function. GetNumOfWords() has a constant string as a parameter and returns the number of words in the string. Hint: Words end when a space is reached except for the last word in a sentence. Call GetNumOfWords() in the PrintMenu() function. (3 pts)

Ex:


(5) Implement the FindText() function, which has two strings as parameters. The first parameter is the text to be found in the user provided sample text, and the second parameter is the user provided sample text. The function returns the number of instances a word or phrase is found in the string. In the PrintMenu() function, prompt the user for a word or phrase to be found and then call FindText() in the PrintMenu() function. Before the prompt, call cin.ignore() to allow the user to input a new string. (3 pts)

Ex:


(6) Implement the ReplaceExclamation() function. ReplaceExclamation() has a string parameter and updates the string by replacing each '!' character in the string with a '.' character. ReplaceExclamation() DOES NOT output the string. Call ReplaceExclamation() in the PrintMenu() function, and then output the edited string. (3 pts)

Ex.


(7) Implement the ShortenSpace() function. ShortenSpace() has a string parameter and updates the string by replacing all sequences of 2 or more spaces with a single space. ShortenSpace() DOES NOT output the string. Call ShortenSpace() in the PrintMenu() function, and then output the edited string. (3 pt)

Ex:

Explanation / Answer

#include <iostream>
using namespace std;

char printMenu();
int GetNumOfNonWSCharacters(string str);
int GetNumOfWords(string str);
int FindText(string str,string sub);
void ReplaceExclamation(string str);
void ShortenSpace(string str);
string text;
int main()
{
  
    cout << "Enter Text : ";
    getline(cin, text);
    cout << "Entered Text : " << text<< endl;
    char c = 'p';
    while(c != 'Q'){
        c = printMenu();
        if(c=='C'||c=='c'){
            int numOfchar = GetNumOfNonWSCharacters(text);
            cout << " Number of non-whitespace characters : " <<numOfchar<<endl;
        }
        if(c=='W'||c=='w'){
            int numOfchar = GetNumOfWords(text);
            cout << " Number of words : " <<numOfchar<<endl;
        }
        if(c=='F'||c=='f'){
            cout << "Enter a word or phrase to be found:";
            string sub;
            cin >> sub;
            int numOfchar = FindText(text,sub);
            cout << " ""<<sub<<"" instances: : " <<numOfchar<<endl;
        }
        if(c=='R'||c=='r'){
            ReplaceExclamation(text);
        }
        if(c=='S'||c=='s'){
            ShortenSpace(text);
        }
        if(c=='q'){
            c ='Q';
        }
    }
   return 0;
}

char printMenu(){
    char choice;
    cout << " [C] Number of non-whitespace characters "<< endl;
    cout << " [W] Number of words "<< endl;
    cout << " [F] Find text "<< endl;
    cout << " [R] Replace all !'s "<< endl;
    cout << " [S] Shorten spaces "<< endl;
    cout << " [Q] Quit"<< endl;
    cout << "Choose an option :";
    cin >> choice;
    return choice;
}

int GetNumOfNonWSCharacters(string str){
    int count=0;
    for(int i=0; i<str.length(); i++){
        char c = str[i];
        if(!isspace(c)){
            count++;
        }
    }
    return count;
}

int GetNumOfWords(string str){
    int count=0;
    for(int i=0; i<str.length(); i++){
        char c = str[i];
        if(isspace(c)){
            count++;
        }
    }
    return count+1;
}
int FindText(string str,string sub){
    int count = 0;
    size_t nPos = str.find(sub, 0); // fist occurrence
    while(nPos != string::npos)
    {
        count++;
        nPos = str.find(sub, nPos+1);
    }
    return count;
}
void ReplaceExclamation(string str){
    int count=0;
    cout <<" Edited text : ";
    for(int i=0; i<str.length(); i++){
        char c = str[i];
        if(c !='!'){
            cout <<c;
        }
    }
    cout <<" ";
}
void ShortenSpace(string str){
    int count=0;
    cout <<" Edited text : ";
    for(int i=0; i<str.length(); i++){
        char c = str[i];
        if(isspace(c))
            cout <<" ";
        else
        cout <<c;
    }
    cout <<" ";
}

//=======================OUTPUT========================

Enter Text : This is my text, i am a programmer!        got it                                                                                                        

Entered Text : This is my text, i am a programmer!      got it                                                                                                        

                                                                                                                                                                      

[C] Number of non-whitespace characters                                                                                                                              

[W] Number of words                                                                                                                                                  

[F] Find text                                                                                                                                                        

[R] Replace all !'s                                                                                                                                                  

[S] Shorten spaces                                                                                                                                                   

[Q] Quit                                                                                                                                                             

Choose an option :r                                                                                                                                                   

                                                                                                                                                                      

Edited text : This is my text, i am a programmer        got it                                                                                                        

                                                                                                                                                                      

[C] Number of non-whitespace characters                                                                                                                              

[W] Number of words                                                                                                                                                  

[F] Find text                                                                                                                                                        

[R] Replace all !'s                                                                                                                                                  

[S] Shorten spaces                                                                                                                                                   

[Q] Quit

Choose an option :c                                                                                                                                                   

                                                                                                                                                                      

Number of non-whitespace characters : 33                                                                                                                              

                                                                                                                                                                      

[C] Number of non-whitespace characters                                                                                                                              

[W] Number of words                                                                                                                                                  

[F] Find text                                                                                                                                                        

[R] Replace all !'s                                                                                                                                                  

[S] Shorten spaces                                                                                                                                                   

[Q] Quit                                                                                                                                                             

Choose an option :w                                                                                                                                                   

                                                                                                                                                                      

Number of words : 11                                                                                                                                                  

                                                                                                                                                                      

[C] Number of non-whitespace characters                                                                                                                              

[W] Number of words                                                                                                                                                  

[F] Find text                                                                                                                                                        

[R] Replace all !'s                                                                                                                                                  

[S] Shorten spaces                                                                                                                                                   

[Q] Quit                                                                                                                                                             

Choose an option :f                                                                                                                                                   

Enter a word or phrase to be found:am                                                                                                                                 

                                                                                                                                                                      

"am" instances: : 2    

     

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote