Question & Answer: The program should start by asking the user to enter a word, a sentence, or a string of numbers. Store whatever the u…..

Write a program that manipulates a string entered by the user.
The program should start by asking the user to enter a word, a sentence, or a string of numbers. Store whatever the user enters into a C++ string.
The program should then display the following menu:
USE THIS MENU TO MANIPULATE YOUR STRING
—————————————-
1) Inverse String
2) Reverse String
3) To Uppercase
4) Jumble String 4-1 Sample Run
5) Count Number Words
6) Count Consonants
7) Enter a Different String
8) Print the String
Q) Quit
¨ If the user selects 1: Inverse the upper and lower case letters of the string. If the string contains numeric characters or special characters do not change them. NOTE: This option should actually change the string to its inverse. Note this option does not display the changed string. If a user wanted to inverse the string and then display the string’s inverse they would select option 1 and then they would select option 8.
Example: If the string is: My name is John and I am 20 years old.
The inverse would be: mY NAME IS jOHN AND i AM 20 YEARS OLD.
¨ If the user selects 2: – Reverse the order of the characters in the string. NOTE: This option should actually change the string to its reverse. Note this option does not display the changed string. If a user wanted to reverse the string and then display the string’s reverse they would select option 2 and then they would select option 8.
Example: If the string is: 2015
The reverse would be: 5102
¨ If the user selects 3: Convert all of the characters in the string to uppercase. If the String contains numeric characters or special characters do not change them. NOTE: This option should actually change the string to all uppercase letters. Note this option does not display the changed string. If a user wanted to change the string to uppercase and then display the new string (in all uppercase) they would select option 3 and then they would select option 8.
¨ If the user selects 4:– Call a function named jumbleString. The jumbleString function takes a string as input and displays a jumbled version of that string. The jumbleString function should be called using current version of the string an argument (input) to the function.
Example: If the string passed to the jumbleString function is: hello
A jumbled version of the word would be: elhlo
Note 1: there are many different jumbled versions of each word that the jumbleString function could display. So elhlo is not the only correct output for the above example.
Note 2: Notice that this option does not actually change the string like the first two menu selections do, the jumbleString function just displays a jumbled version of the string rather than actually changing the string
¨ If the user selects 5: Call a function named countWords that counts the number of words in the current string and displays a message stating how many words are in the string
Examples: The string “2015” has one word
The string “Hello World” has two words
The string “ I am Woman ” has 3 words
¨ If the user selects 6: Call a function named countConsonants that counts the number of consonants in the current string and displays a message indicating how many consonants the string contains. Consonants are letters that aren’t vowels.
Example: If the string is: Hello
The number of consonants is: 3
So display: “The number of consonants in the string is 3.”
¨ If the user selects 7: Let the user enter another string for processing. (This should just change the string stored in the original string variable you created at the beginning of the program)
¨ If the user selects 8: Print the String
o So if the original string was “Hello” and the user processed the string with option 3 followed by option 2, followed by option 8, the string would print out as “OLLEH” (This is hello first converted to uppercase and then reversed).
¨ If the user selects ‘Q’ or ‘q’: Quit the program
¨ Allow the user to continue processing strings (using the menu) until they select ‘Q’ or ‘q’ to quit
¨ If the user makes an invalid menu selection, print an error message to the screen
Your program must include the following three functions (you can include more if you want):
1. countWords: A function that takes a string as an argument and counts the number of words in it (this function should return an int which is the number of words in the string)
2. countConsonants: A function that takes a string as an argument and counts the number of consonants in it (this function should return an int which is the number of consonants in the string)
3. jumbleString: A function that take a string as an argument and displays a jumbled version of the original string.

Expert Answer

 

/*The source code is given below:*/

#include <iostream>

#include<cstring>

#include<algorithm>

using namespace std;

void jumbleString(string s);

void countWords(string s);

void countConsonants(string str);

int main()

{

char option; // user’s entered option will be saved in this variable

string myStr;//user’s entered string will be saved in this variable

cout<<endl<<“Enter input string: “;

getline(cin,myStr);

do //do-while loop displays menu again and again until user select to exit

{

//Displaying Options for the menu

cout<<endl<<“===========Menu============”<<endl;

cout << “1) Inverse String” << endl;

cout << “2) Reverse String ” << endl;

cout << “3) To Uppercase” << endl;

cout << “4) Jumble String ” << endl;

cout << “5) Count Number Words ” << endl;

cout << “6) Count Consonants ” << endl;

cout << “7) Enter a Different String ” << endl;

cout << “8) Print the String ” << endl;

cout << “Q) Quit ” << endl;

//Prompting user to enter an option according to menu

cout << “Please select an option : “;

cin>>option; // taking option value as input and saving in variable “option”

if(option == ‘1’) // Checking if user selected option 1

{

//inversing string

for(int i=0;i<myStr.length();i++)

{

if(isupper(myStr[i]))

myStr[i]=tolower(myStr[i]);

else if(islower(myStr[i]))

myStr[i]=toupper(myStr[i]);

else

continue;

}

}

else if(option == ‘2’) // Checking if user selected option 2

{

//reversing the string

reverse(myStr.begin(),myStr.end() );

}

else if(option == ‘3’) // Checking if user selected option 3

{

//converting all letters to uppercase

for(int i=0;i<myStr.length();i++)

{

if(isupper(myStr[i]))

continue;

else if(islower(myStr[i]))

myStr[i]=toupper(myStr[i]);

else

continue;

}

}

else if(option == ‘4’) // Checking if user selected option 4

{

//calling a function to display jumbled version of the input string

jumbleString(myStr);

}

else if(option == ‘5’) // Checking if user selected option 5

{

//calling a function to display number of words in the input string

countWords(myStr);

}

else if(option == ‘6’) // Checking if user selected option 6

{

//calling a function to display number of consonants in the input string

countConsonants(myStr);

}

else if(option == ‘7’) // Checking if user selected option 7

{

//let the user enter another string for processing

cin.clear();cin.ignore(INT_MAX,’n’);//flush out cin buffer

cout<<endl<<“Enter another input string: “<<endl;

getline(cin,myStr);

}

else if(option == ‘8’) // Checking if user selected option 3

{

//displaying the string

cout << endl<<myStr<< endl;

}

else if((option == ‘Q’) || (option==’q’) )// Checking if user selected option q or Q

{

cout << “Thank you!!” << endl;

}

else //if user has entered invalid choice (other than 1 to 8 ,q, Q)

{

//Displaying error message

cout << “Invalid Option entered!!” << endl;

}

}while((option != ‘q’) && (option != ‘Q’ )); //condition of do-while loop

return 0;

}

void jumbleString(string s)

{

string str=s;

random_shuffle(str.begin(), str.end());

cout <<endl<< str << endl;

}

void countWords(string str)

{

int words = 0; // Holds number of words

for(int i = 0; i<str.length(); i++)

{

if (str[i] == ‘ ‘) //Checking for spaces

{

words++;

}

}

cout << “The string “”<<str<<“” has “;

if(words+1 == 1)

cout<<words+1<<” word”<<endl;

else

cout<<words+1<<” words”<<endl;

}

void countConsonants(string str)

{

int cons = 0; // Holds number of consonants

for(int i = 0; i<str.length(); i++)

{

char ch = str[i];

if (isalpha(str[i])) //Checking for alphabet

{

if(ch==’a’ || ch==’A’ || ch==’e’ || ch==’E’ ||

ch==’i’ || ch==’I’ || ch==’o’ || ch==’O’ ||

ch==’u’ || ch==’U’)

{

continue;

}

else

{

cons++;

}

}

}

cout << “The number of consonants in the string is “<<cons<<endl;

}

/*For better understanding output screenshot is attached here*/

Still stressed from student homework?
Get quality assistance from academic writers!