Question

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

(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 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. (3 pts)

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. (4 pts)

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. (3 pts)

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. (3 pts)

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

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

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!


Please revise my code. I keep getting this error in Zybooks when I try to run my code:
Program generated too much output. Output restricted to 50000 characters. Check program for any unterminated loops generating output.


Here is my code:

#include <iostream>

#include <string>

using namespace std;

//Function prototypes

char PrintMenu(string);

int GetNumOfNonWSCharacters(string);

int GetNumOfWords(string);

void ReplaceExclamation(string&);

void ShortenSpace(string&);

int FindText(string, string);

//Main function

int main()

{

   //variable decalaration

   char option;

   string text, phraseToFind;

   //Reading text from user

   cout << "\n\n Enter a sample text: ";

   getline(cin, text);

   //Printing text

   cout << "\n\n You entered: " << text;

   //Loop till user wants to quit

   do

   {

       //Printing menu

       option = PrintMenu(text);

       cout << "\n\n";

   } while (option == 'Q' || 'q');

   system("pause");

   return 0;

}

//Function that prints menu

char PrintMenu(string text)

{

   char ch;

   string phraseToFind;

   //Printing menu

   cout << "\n\n\t Menu Options: \n";

   cout << "\n \t c - Number of non-whitespace characters \n\t w - Number of words \n\t f - Find text \n\t r - Replace all !'s \n\t s - Shorten spaces \n\t q - Quit";

   cout << "\n\n\t Choose an option: ";

   //Reading user choice

   cin >> ch;

   //Calling functions based on option selected by //user

   switch (ch)

   {

       //User wants to quit

   case 'q':

   case 'Q':

       exit(0);

       //Counting non-whitespace characters

   case 'c':

   case 'C':

       cout << "\n\n Number of non-whitespace characters: " << GetNumOfNonWSCharacters(text) << "\n\n";

       break;

       //Counting number of words

   case 'w':

   case 'W':

       cout << "\n\n Number of words: " << GetNumOfWords(text) << "\n\n";

       break;

       //Counting number of occurrences phrase in //given string

   case 'f':

   case 'F':

       cin.ignore();

       cout << "\n\n Enter a word/Phrase to find: ";

       getline(cin, phraseToFind);

       cout << "\n\n \"" << phraseToFind << "\" instances: " << FindText(text, phraseToFind) << " \n\n";

       break;

       //Replacing ! with .

   case 'r':

   case 'R': ReplaceExclamation(text); cout << "\n\n Edited text: " << text << "\n\n";

       break;

       //Replacing multiple spaces with single //space

   case 's':

   case 'S': ShortenSpace(text); cout << "\n\n Edited text: " << text << "\n\n";

       break;

   default:

       cout << "\n\n Invalid Choice.... Try Again \n\n";

       break;

   }

   return ch;

}

//Function that count number of non space characters

int GetNumOfNonWSCharacters(const string text)

{

   int cnt = 0, i;

   int len = text.size();

   //Looping over given text

   for (i = 0; i

   {

       //Counting spaces

       if (!isspace(text[i]))

           cnt++;

   }

   return cnt;

}

//Function that count number of words in the string

int GetNumOfWords(const string text)

{

   int words = 0, i;

   int len = text.size();

   //Looping over text

   for (i = 0; i

   {

       //Checking for space

       if (isspace(text[i]))

       {

           //Handling multiple spaces

           while (isspace(text[i]))

               i++;

           //Incrementing words

           words++;

       }

       else

       {

           i++;

       }

   }

   //Handling last word

   words = words + 1;

   return words;

}

//Function that replaces ! with .

void ReplaceExclamation(string& text)

{

   string newText = text;

   int i, len = text.size();

   //Looping over string

   for (i = 0; i

   {

       //Replacing ! with .

       if (text[i] == '!')

           newText[i] = '.';

   }

   text = newText;

}

//Function that replaces Multiple spaces with single space

void ShortenSpace(string& text)

{

   char *newText;

   int i, len = text.size(), k = 0;

   newText = new char[len + 1];

   //Looping over string

   for (i = 0; i

   {

       //Assign individual characters

       newText[k] = text[i];

       //Handling multiple spaces

       if (isspace(text[i]))

       {

           //Replacing multiple spaces with single //space

           while (isspace(text[i]))

               i++;

       }

       else

       {

           i++;

       }

   }

   newText[k] = '\0';

   text = newText;

}

//Function that counts the occurrences of given phrase in a //given text

int FindText(string text, string phrase)

{

   int count = 0;

   if (phrase.size() == 0)

       return 0;

   //Counting number of phrase occurrences in the given string

   for (size_t offset = text.find(phrase); offset != string::npos; offset = text.find(phrase, offset + phrase.size()))

   {

       ++count;

   }

   //Retuning count

   return count;

}

**I understand it works for other compilers, however it does not work on zybooks.**

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <cstdlib>

#include <string>

using namespace std;

//Function prototypes

char PrintMenu(string);

int GetNumOfNonWSCharacters(string);

int GetNumOfWords(string);

void ReplaceExclamation(string &);

void ShortenSpace(string &);

int FindText(string, string);

//Main function

int main()

{

//variable decalaration

char option;

string text, phraseToFind;

//Reading text from user

cout << "\n\n Enter a sample text: ";

getline(cin, text);

//Printing text

cout << "\n\n You entered: " << text;

//Loop till user wants to quit

do

{

//Printing menu

option = PrintMenu(text);

cout << "\n\n";

} while (option != 'Q' && option != 'q');

//system("pause");

return 0;

}

//Function that prints menu

char PrintMenu(string text)

{

char ch;

string phraseToFind;

//Printing menu

cout << "\n\n\t Menu Options: \n";

cout << "\n \t c - Number of non-whitespace characters \n\t w - Number of words \n\t f - Find text \n\t r - Replace all !'s \n\t s - Shorten spaces \n\t q - Quit";

cout << "\n\n\t Choose an option: ";

//Reading user choice

cin >> ch;

//Calling functions based on option selected by //user

switch (ch)

{

//User wants to quit

case 'q':

case 'Q':

exit(0);

//Counting non-whitespace characters

case 'c':

case 'C':

cout << "\n\n Number of non-whitespace characters: " << GetNumOfNonWSCharacters(text) << "\n\n";

break;

//Counting number of words

case 'w':

case 'W':

cout << "\n\n Number of words: " << GetNumOfWords(text) << "\n\n";

break;

//Counting number of occurrences phrase in //given string

case 'f':

case 'F':

cin.ignore();

cout << "\n\n Enter a word/Phrase to find: ";

getline(cin, phraseToFind);

cout << "\n\n \"" << phraseToFind << "\" instances: " << FindText(text, phraseToFind) << " \n\n";

break;

//Replacing ! with .

case 'r':

case 'R':

ReplaceExclamation(text);

cout << "\n\n Edited text: " << text << "\n\n";

break;

//Replacing multiple spaces with single //space

case 's':

case 'S':

ShortenSpace(text);

cout << "\n\n Edited text: " << text << "\n\n";

break;

default:

cout << "\n\n Invalid Choice.... Try Again \n\n";

break;

}

return ch;

}

//Function that count number of non space characters

int GetNumOfNonWSCharacters(const string text)

{

int cnt = 0, i;

int len = text.size();

//Looping over given text

for (i = 0; i<len; i++)

{

//Counting spaces

if (!isspace(text[i]))

cnt++;

}

return cnt;

}

//Function that count number of words in the string

int GetNumOfWords(const string text)

{

int words = 0, i;

int len = text.size();

//Looping over text

for (i = 0; i < len; i++)

{

//Checking for space

if (isspace(text[i]))

{

//Handling multiple spaces

while (isspace(text[i]))

i++;

//Incrementing words

i--;

words++;

}

}

//Handling last word

words = words + 1;

return words;

}

//Function that replaces ! with .

void ReplaceExclamation(string &text)

{

string newText = text;

int i, len = text.size();

//Looping over string

for (i = 0; i < len; i++)

{

//Replacing ! with .

if (text[i] == '!')

newText[i] = '.';

}

text = newText;

}

//Function that replaces Multiple spaces with single space

void ShortenSpace(string &text)

{

int i, len = text.size(), k = 0;

string newText = "";

//Looping over string

for (i = 0; i < len; i++)

{

//Assign individual characters

//Handling multiple spaces

if (isspace(text[i]))

{

//Replacing multiple spaces with single //space

while (isspace(text[i]))

i++;

i--;

newText += " ";

}

else

{

newText += text[i];

}

}

text = newText;

}

//Function that counts the occurrences of given phrase in a //given text

int FindText(string text, string phrase)

{

int count = 0;

if (phrase.size() == 0)

return 0;

//Counting number of phrase occurrences in the given string

for (size_t offset = text.find(phrase); offset != string::npos; offset = text.find(phrase, offset + phrase.size()))

{

++count;

}

//Retuning count

return count;

}

Let me know if you have any clarifications. Thank you

Add a comment
Know the answer?
Add Answer to:
(1) Prompt the user to enter a string of their choosing. Store the text in a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 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!...

  • C++ please! (1) Prompt the user to enter the name of the input file. The file...

    C++ please! (1) Prompt the user to enter the name of the input file. The file will contain a text on a single line. Store the text in a string. Output the string. Ex: Enter file name: input1.txt File content: 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,...

  • The code keeps saying its generating too much output check for any unterminated loops. I cant...

    The code keeps saying its generating too much output check for any unterminated loops. I cant find any. Can anyone help? #include <iostream> #include <string> using namespace std; char printMenu(); int GetNumOfNonWSCharacters(string); int GetNumOfWords(string); void ReplaceExclamation(string&); void ShortenSpace(string&); int FindText(string, string); int main(){ char option; string text, phraseToFind; cout << "\n\n Enter a sample text: "; getline(cin, text); cout << "\n\n You entered: " << text; while(1){ option = printMenu(); switch(option){ case 'q': case 'Q': return 0; case 'c': case...

  • Program: Authoring assistant

    7.17 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 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() method, which 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() method....

  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • (7) Implement the shorten_space() function. shorten_space() has a string parameter and updates the string by replacing...

    (7) Implement the shorten_space() function. shorten_space() has a string parameter and updates the string by replacing all sequences of 2 or more spaces with a single space. shorten_space() returns the string. Call shorten_space() in the print_menu() function, and then output the edited string. Hint: Look up and use Python function .isspace(). 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....

  • 6.17.1 Lab #5 - Chapter 6 Text analyzer & modifier (C) (1) Prompt the user to...

    6.17.1 Lab #5 - Chapter 6 Text analyzer & modifier (C) (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() function, which returns the number of characters in the user's string. We encourage you to use a for loop in this...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • Here is the beginning of the code; #include <string> #include <iostream> int main() { std::string sentence;...

    Here is the beginning of the code; #include <string> #include <iostream> int main() { std::string sentence; std::getline(std::cin, sentence); // manipulate the sentence here std::cout << sentence << "\n"; }} Lab 4.5.1 Text manipulation: duplicate white space Objectives Familiarize the student with: • the basics of string and text manipulation. Scenario Write a program that will read a line of text and remove all duplicate whitespace. Note that any single whitespace characters must remain intact. #include <string> #include <iostream> int main()...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT