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

6.21 Prep Lab: Authoring assistant (C++) (1) Prompt the user to enter a string o

ID: 3722607 • Letter: 6

Question

6.21 Prep Lab: Authoring assistant (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 passes the user entered 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 user provided sample text, and the second parameter is the text to be found in 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() is void. It takes 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() is void. It takes 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>

#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] = '';

len--;

}

i++;

}

}

void PrintMenu(){

string str = "We'll !contin ue 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!";

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;

char choice;

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;

}

}

int main(){

PrintMenu();

return 0;

}