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*/