Question

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;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include<string.h>

using namespace std;

// function to calculate number non white space characters
int GetNumOfNonWSCharacters(string str) {
   int i = 0;
   int count = 0;
   while(str[i] != '\0') {
      if(str[i] != ' ') {
         count += 1;
      }
      i++;
   }
   return count;
}

// function to calculate numbers of words
int GetNumOfWords(string str) {
   int i = 0;
   int count = 1;
   while(str[i] != '\0') {
      if(str[i] == ' ' && str[i-1] != ' ') {
         count += 1;
      }
      i++;
   }
   return count;
}


// function to find the number of occurence of the word
int FindText(string word, string str) {
   int i = 0;
   int count = 0;
   while(str[i] != '\0') {
      if(str[i] == word[0]) {
         int k = i;
         int j = 0;
         while(word[j] != '\0') {
            if(word[j] != str[k]) {
               break;
            }
            if(word[j+1] == '\0') {
               count += 1;
            }
            k++;
            j++;
         }
      }
      i++;
   }
   return count;
}


// function to replace the '!' in string
void ReplaceExclamation(string str) {
   int i = 0;
   string edited;
   while(str[i] != '\0') {
      if(str[i] == '!') {
         edited[i] = '.';
      }
      else {
         edited[i] = str[i];
      }
      i++;
   }
   edited[i] = '\0';

   // display resulting string
   int j = 0;
   cout << "Edited Text:\n" << endl;
   while(edited[j] != '\0') {
      cout << edited[j];
      j++;
   }
   cout << endl;
}


// function to remove extra spaces
void ShortenSpace(string str) {
   int i = 0;
   int j = 0;
   int len = str.length();
   string edited;
   while(str[i] != '\0') {
      if(str[i] != ' ') {
         edited[j] = str[i];
         j++;
      }
      else {
         if(edited[j-1] != ' ') {
            edited[j] = ' ';
            j++;
         }
         
      }
      i++;
   }
   edited[j] = '\0';

   // display resulting string
   int k = 0;
   cout << "Edited Text:\n" << endl;
   while(edited[k] != '\0') {
      cout << edited[k];
      k++;
   }
   cout << endl;
}


// function to display menu options
void PrintMenu(string str) {
   while(1) {

      // display menu
      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;

      // read user option
      char option;
      cout << "Choose an option: ";
      cin >> option;
      
      // validate input
      while(1) {
         if(option != 'c' && option != 'w' && option != 'r' && option != 's' && option != 'q' && option != 'f') {
            cout << "Choose an option: ";
            cin >> option;
         }
         else{
            break;
         }
      }

      // perform the required operation
      if(option == 'q') {
         break;
      }

      else if(option == 'c') {
         int non_whitespace_chars = GetNumOfNonWSCharacters(str);
         cout << "Number of non-whitespace characters: " << non_whitespace_chars << endl;
      }

      else if(option == 'w') {
         int num_words = GetNumOfWords(str);
         cout << "Number of words: " << num_words << endl;
      }

      else if(option == 'f') {
         string word;
         cin.ignore();
         getline(cin, word);
         int finds = FindText(word, str);
         cout << "Number of words or phrases found: " << finds << endl;
      }

      else if(option == 'r') {
         ReplaceExclamation(str);
      }

      else {
         ShortenSpace(str);
      }

      cout << endl;
   }
}


// main function
int main() {

   // read input text and output it
   string text;
   cout << "Enter sample text:" << endl;
   getline(cin, text);
   cout << "You entered:\n" << text << endl;

   // call print menu function 
   PrintMenu(text);

   return 0;
}

FOR HELP PLEASE COMMENT.
THANK YOU.

Add a comment
Know the answer?
Add Answer to:
C++ (1) Prompt the user to enter a string of their choosing (Hint: you will need...
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++ 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,...

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

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

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

  • In C# You will be prompting the user for a text string a list of bills...

    In C# You will be prompting the user for a text string a list of bills they have every month. a. This should be a comma separated list , bill1, bill2, bill3 b. Validate that this text string is not left blank. 3. Create a custom function called CreateBillArray a. This function should accept the string variable that holds the bills. b. Inside of the function, split the text string of the bills into a string array with each individual...

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • Program: Playlist (C++) I'm having difficulty figuring out how to get the header file to work....

    Program: Playlist (C++) I'm having difficulty figuring out how to get the header file to work. 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. Playlist.h - Class declaration Playlist.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...

  • Need help with this homework, and follow the bolded text required 7.10 LAB: Data Visualization (1) Write a function, get_data_headers(), to prompt the user for a title, and column headers for a table....

    Need help with this homework, and follow the bolded text required 7.10 LAB: Data Visualization (1) Write a function, get_data_headers(), to prompt the user for a title, and column headers for a table. Return a list of three strings, and print the title, and column headers. (2 pt) Ex: Enter a title for the data: Number of Novels Authored You entered: Number of Novels Authored Ex: Enter the column 1 header: Author name You entered: Author name Enter the column...

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