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

Write a C++ program that manipulates a string entered by the user. The program s

ID: 3721811 • Letter: W

Question

Write a C++ 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 5) Count Number Words 6) Count Consonants 7) Enter a Different String 8) Print the String 9) Quit

Option 1) Inverse the upper and lower case letters of the string. If the string contains numeric characters or special characters do not change them. Option 2) Reverse the order of the characters in the string. Option 3) Convert all of the characters in the string to uppercase Option 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 Option 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 "2016" is one word "I am bill" is 3 words Option 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. (non vowels) Option 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 Option 8) Prints the String Option 9) Quits program

Explanation / Answer

#include <iostream>

#include <iomanip>

#include <cctype>

#include <cstring>

#include <string.h>

using namespace std;

const int MaxChar = 81;  

int countWords(char string[MaxChar]);

int countConsonant(char string[MaxChar]);

void jumbleString(char string[MaxChar],int length);

void inverse();

int main()

{

int choice = 0; //declaring variable choice that store the choice of the user

char string[MaxChar]; //declaring an array in which the user enter the string,

char string2[MaxChar]; //declaring a second array needed in some manipulation

int count = 0, numofwords, numofconsonant; // declaring a count varible, and two other variables that store the number of words and the number of constant

  

cout << "Enter a string: "; //asking user to enter the string

cin.getline(string, MaxChar, ' '); //getting the string from the user

int length = strlen(string); //setting a variable that contain the length of the string it will be needed

int end = length - 1;

string2[length] = '';

bool quit =false;

while (!quit) //a loop where user cant come out unless user presses 9 to quit

{

cout << " USE THIS MENU TO MANIPULATE YOUR STRING" << endl //printing out to the screen the options that the user can use

<< "--------------------------------------" << endl

<< "1) Inverse String"<<endl

<< "2) Reverse String" << endl

<< "3) To uppercase" << endl

<< "4) Jumble String" << endl

<< "5) Count number words" << endl

<< "6) Count consonants" << endl

<< "7) Enter different String" << endl

<< "8) Print the String" << endl

<< "9) Quit"<<endl;

cin >> choice;

switch (choice) // a switch statement to determine what to do in which case

{

case 1:

strcpy(string2,string);

count = 0;

while (string2[count] != '')

{

if (islower(string2[count]))

{

string2[count] = toupper(string2[count]);

}

else if (isupper(string[count]))

{

string2[count] = tolower(string2[count]);

}

count++;

}

cout << "The String is inversed" << endl;

break;

case 2:

for (int count=0; count != length; count++)

{

string2[count] = string[end];

end --;

}

string2[length] = '';

strcpy(string, string2);

cout << "The String is reversed" << endl;

break;

case 3:

strcpy(string2,string);

for (int position=0; string[position] != ''; position++)

{

string[position] = toupper(string[position]);

}

cout << "The string is changed to uppercase" << endl;

break;

case 4:

strcpy(string2,string);

jumbleString(string2,length);

break;

case 5:

strcpy(string2,string);

numofwords = countWords(string2);

cout << "The number of words is " << numofwords << endl;

break;

case 6:

strcpy(string2,string);

numofconsonant = countConsonant(string2);

cout << "The number of consonant is " << numofconsonant << endl;

break;

case 7:

cout << "The other string is: ";

cin.ignore();

cout << "Enter a string: "; //asking user to enter the string

cin.getline(string, MaxChar, ' '); //getting the string from the user

break;

case 8:

cout << "The String is: " << string2 << endl;

break;

case 9:

cout << "Quiting"<< endl;

quit = true;

break;

default: // catch bad input

std::cout << "Bad Input Try Again ";

break;

}

}

return 0;

}

int countWords(char string[MaxChar])

{

int numofwords = 0;

for (int position=0; string[position] != ''; position++)

{

if (isspace(string[position+1])&&!isspace(string[position]))

{

numofwords++;

}

}

if (string[0] != ' ')

{

numofwords++;

}

return numofwords;

}

int countConsonant(char string[MaxChar])

{

int numofconsonant = 0;

int end = strlen(string) - 1;

for (int position=0; string[position] != ''; position++)

{

if (tolower(string[position]) == 'b'

|| tolower(string[position]) == 'd'

|| tolower(string[position]) == 'c'

|| tolower(string[position]) == 'd'

|| tolower(string[position]) == 'f'

|| tolower(string[position]) == 'g'

|| tolower(string[position]) == 'h'

|| tolower(string[position]) == 'j'

|| tolower(string[position]) == 'k'

|| tolower(string[position]) == 'l'

|| tolower(string[position]) == 'm'

|| tolower(string[position]) == 'n'

|| tolower(string[position]) == 'p'

|| tolower(string[position]) == 'q'

|| tolower(string[position]) == 'r'

|| tolower(string[position]) == 's'

|| tolower(string[position]) == 't'

|| tolower(string[position]) == 'v'

|| tolower(string[position]) == 'w'

|| tolower(string[position]) == 'x'

|| tolower(string[position]) == 'y'

|| tolower(string[position]) == 'z')

{

++numofconsonant;

}

}

return numofconsonant;

}

void jumbleString(char string[MaxChar],int length)

{

char string2[MaxChar]; // an aray to store the jumbled string in then copy it back to the main array

string2[length] = '';

int value[MaxChar]; //array to store the random numbers in in

value[length] = '';

int x; //variable to store random numbers in

int start = 0;

bool check; //boolean to check or number is already used

srand(time(NULL)); //generate random numbers:

for (int count=0; count<length; count++) //count the number of characcters in array setting a value for each one

{

do{

x = rand() % length;

check = true; //assuming the boalean is true

for (int start = 0; start < length; start++) //check or number is already used:

{

if (x == value[start]) //if number is already used

{

check = false; //set check to false

break; //no need to check the other elements of value[]

}

}

} while (!check); //loop until new, unique number is found

value[count] = x; //store the generated number in the array

string2[count] = string[x]; // use the number to set a value in the array

}

cout << "The jumbled string is: " << string2 << endl; //print the jumbled version to the screen

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote