Please write this is C++ (1) Prompt the user to enter a string of their choosing
ID: 3814724 • Letter: P
Question
Please write 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
Assumptions :
1. There is only one white space between two words.
For explanation, please check comments
#include<iostream>
#include<stdlib.h>
#include<conio.h>
#define MAX_CHAR 10000
using namespace std;
void printMenu(char str[],int len);
int GetNumOfNonWSCharacters(char str[],int len);
int GetNumOfWords(char str[], int len);
int main()
{
char *str,*p;
int len=0;
str = (char*)malloc(sizeof(char)*MAX_CHAR); // Allocate memory to char pointer
cout<<"Enter the string ";
gets(str); // Get the string from user input
cout<<" You Entered : ";
puts(str); // Print the string on the screen
p=str;
while(*p!='') // loop to get the length of string
{
len++;
p++;
}
printMenu(str,len); // Call printMenu function
}
void printMenu(char str[],int len)
{
char ch;
int white_space,number_of_words;
cout<<" MENU "; // Show menu options
cout<<"c - Number of non-whitespace characters ";
cout<<"w - Number of words ";
cout<<"f - Find text ";
cout<<"r - Replace all !'s ";
cout<<"s - Shorten spaces ";
cout<<"q - Quit";
cout<<"Enter your choice ";
fflush(stdin); // clear the input buffer for safer side;
cin>>ch;
switch(ch) // Put the switch case to check the input character value
{
case 'c' : // If ch == c then count number of non white space character
white_space= GetNumOfNonWSCharacters(str,len);
cout<<"Number of non-whitespace characters are "<<white_space<<" ";
printMenu(str,len);
break;
case 'w' : // If ch == w then count number of words
number_of_words=GetNumOfWords(str,len);
cout<<"Number of words are "<<number_of_words<<" ";
printMenu(str,len);
break;
case 'f' : // As per the rule of chegg, I have answered first four parts.
break;
case 'r' :
break;
case 's' :
break;
case 'q' :
break;
default:
cout<<" Invalid Character. Please enter again";
printMenu(str,len);
break;
}
}
int GetNumOfNonWSCharacters(char str[],int len) // Definition of GetNumOfNonWSCharacters
{
int ws_spc = 0,i ;
for(i=0;i<len;i++)
{
if(str[i]!=32) // count number of characters other than white space in loop
ws_spc++;
}
return ws_spc; // return the value
}
int GetNumOfWords(char str[], int len) // Definition of GetNumOfWords
{
int num_words = 0,i;
for(i=0;i<len;i++)
{
if(str[i]==32) // count number of white space in loop
num_words++;
}
num_words++; // Number of words == Number of white space + 1;
return num_words; // return the value
}