Question

In: Computer Science

C++ (1) Prompt the user to enter a string of their choosing (Hint: you will need...

C++

(1) Prompt the user to enter a string of their choosing (Hint: you will need to call the getline() function to read a string consisting of white spaces.) Store the text in a string. Output the string. (1 pt)

Ex:

Enter a sample text:
We'll continue our quest in space.  There will be more shuttle flights and more shuttle crews and,  yes,  more volunteers, more civilians,  more teachers in space.  Nothing ends here;  our hopes and our journeys continue!

You entered: We'll continue our quest in space.  There will be more shuttle flights and more shuttle crews and,  yes,  more volunteers, more civilians,  more teachers in space.  Nothing ends here;  our hopes and our journeys continue!


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

More specifically, the PrintMenu() function will consist of the following steps:

  • print the menu
  • receive an end user's choice of action (until it's valid)
  • call the corresponding function based on the above choice



Ex:

MENU
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:


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

Ex:

Number of non-whitespace characters: 181


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

Ex:

Number of words: 35


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

Ex:

Enter a word or phrase to be found:
more
"more" instances: 5


(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 return the revised string. Call ReplaceExclamation() in the PrintMenu() function, and then output the edited string.

Ex.

Edited text: We'll continue our quest in space.  There will be more shuttle flights and more shuttle crews and,  yes,  more volunteers, more civilians,  more teachers in space.  Nothing ends here;  our hopes and our journeys continue.


(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 return the revised string. Call ShortenSpace() in the PrintMenu() function, and then output the edited string.

Ex:

Edited text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!

______________________________________________

#include <iostream>
#include <string>
using namespace std;

int main() {

/* Type your code here. */

return 0;
}

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include <iostream>

#include <cstdlib>

using namespace std;

int GetNumOfNonWSCharacters(string str)

{

    int i = 0, len = str.length(), count = 0;

    while (i < len)

    {

        if (str[i] != ' ')

            count++;

        i++;

    }

    return count;

}

int GetNumOfWords(string str)

{

    int i = 0, len = str.length(), count = 0, prev = 0;

    while (i < len)

    {

        if (str[i] == ' ' && str[i - 1] != ' ')

        {

            count++;

        }

        i++;

    }

    return count + 1;

}

int FindText(string str, string word)

{

    int i = 0, len = str.length(), instances = 0, w_len = word.length();

    while (i < len - w_len)

    {

        int j = 0, count = 0;

        while (j < w_len)

        {

            if (str[i + j] == word[j])

            {

                count++;

            }

            j++;

        }

        if (count == w_len)

        {

            instances++;

        }

        i++;

    }

    return instances;

}

void ReplaceExclamation(string *str)

{

    int i = 0, len = (*str).length();

    while (i < len)

    {

        if ((*str)[i] == '!')

        {

            (*str)[i] = '.';

        }

        i++;

    }

}

void ShortenSpace(string *str)

{

    int i = 0, len = (*str).length();

    while (i < len)

    {

        if ((*str)[i] == ' ' && (*str)[i + 1] == ' ')

        {

            cout << "in " << endl;

            int j = i;

            while (j < len)

            {

                (*str)[j] = (*str)[j + 1];

                j++;

            }

            (*str)[j] = '\0';

            len--;

        }

        i++;

    }

}

void PrintMenu(string &str)

{

    char choice;

    do

    {

        cout << "MENU" << endl;

        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;

        switch (choice)

        {

        case 'c':

            cout << "Number of non-whitespace characters: " << GetNumOfNonWSCharacters(str) << endl;

            break;

        case 'w':

            cout << "Number of Words: " << GetNumOfWords(str) << endl;

            break;

        case 'f':

        {

            string word;

            cout << "Enter a word or phrase to be found: ";

            getline(cin, word);

            getline(cin, word);

            cout << "\"" << word << "\" instances: " << FindText(str, word) << endl;

            break;

        }

        case 'r':

            ReplaceExclamation(&str);

            cout << "Edited text: " << str << endl;

            break;

        case 's':

            ShortenSpace(&str);

            cout << "Edited text: " << str << endl;

            break;

        case 'q':

            cout << "Thank you!" << endl;

            exit(1);

        default:

            cout << "Invalid option" << endl;

        }

    }while(choice!='q');

}

int main()

{

    string str;

    cout << "Enter a sample text: " << endl;

    getline(cin, str);

    cout << "You entered: " << str << endl;

    PrintMenu(str);

    return 0;

}


Related Solutions

C code please (1) Prompt the user to enter a string of their choosing. Store the...
C code please (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be more shuttle flights and...
*******************In Python please******************* (1) Prompt the user to enter a string of their choosing. Store the...
*******************In Python please******************* (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be more shuttle flights and more...
6.30 LAB*: Program: Authoring assistant (1) Prompt the user to enter a string of their choosing....
6.30 LAB*: Program: Authoring assistant (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be more shuttle...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
5.23 LAB: Warm up: Text analyzer & modifier (1) Prompt the user to enter a string...
5.23 LAB: Warm up: Text analyzer & modifier (1) Prompt the user to enter a string of their choosing. Output the string. (1 pt) Ex: Enter a sentence or phrase: The only thing we have to fear is fear itself. You entered: The only thing we have to fear is fear itself. (2) Complete the getNumOfCharacters() method, which returns the number of characters in the user's string. We encourage you to use a for loop in this function. (2 pts)...
8.30 LAB*: Program: Authoring assistant. PYTHON PLEASE!! (1) Prompt the user to enter a string of...
8.30 LAB*: Program: Authoring assistant. PYTHON PLEASE!! (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be...
use c++ (1) Prompt the user for a string that contains two strings separated by a...
use c++ (1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Print an error message if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. (2 pts) Ex:...
Assignment in C: prompt the user to enter secret message that is terminated by presding Enter....
Assignment in C: prompt the user to enter secret message that is terminated by presding Enter. You can assume that the the length of this message will be less than 100 characters. You will then parae this message, character by character, converting them to lower case, and find corresponding characters in the words found in the key text word array. Once a character match is found, you will write the index of the word and the index of the character...
Create in C++ Prompt the user to enter a 3-letter abbreviation or a day of the...
Create in C++ Prompt the user to enter a 3-letter abbreviation or a day of the week and display the full name of the day of the week. Use an enumerated data type to solve this problem. Enumerate the days of the week in a data type. Start with Monday and end with Friday. Set all of the characters of the user input to lower case. Set an enumerated value based on the user input. Create a function that displays...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT