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

**IMPORTANT!!: C++ only Note you must use the string operators and CHRACTER func

ID: 3739042 • Letter: #

Question

**IMPORTANT!!: C++ only

Note you must use the string operators and CHRACTER functions we discussed in class

(Chapter 14 supplement) to solve this problem (examples: the subscript operator and the isupper function).

Do not use the standard template library functions in cases where you can solve the problem based

on what we learned in lecture

– the point of the program is to utilize the material we learned in class. Only

use the functions like isalpha, tolower, and so on

Write a program that manipulates a string entered by the user.

1. 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 (note: main will have only ONE string)

2. The program should then display the following menu (you must use ‘Q’ for quit):

USE THIS MENU TO MANIPULATE YOUR STRING

----------------------------------------

1) Inverse String

2) Reverse String

3) To Uppercase

4) Count Number Words

5) Count Consonants

6) Enter a Different String

7) Print the String

0) Quit

?

If the user selects 1:

Make a function to 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 7.

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:

– Make a function to 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 7.

Example:

If the string is:

2015

The inverse would be:

5102

?

If the user selects 3

: Make a function to 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 7.

?

If the user selects 4

: Call a function named

countWords

that counts the number of words in the current

string and returns how many words are in the string. This option (not the function) actually should print

something.

Examples:

The string “2015” has one word

The string “Hello World” has two words

?

If the user selects 5:

Call a function named

countConsonants

that counts the number of consonants in the

current string and returns how many consonants the string contains. So this option (not the function) does print

something. Consonants are letters that aren’t vowels (anything EXCEPT a, e, i, o, u).

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 6:

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 ... no function required)

?

If the user selects 7:

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 7, the string would print out as “OLLEH” (This is hello first converted

to uppercase and then reversed ... no function required).

?

If the user selects ‘0’:

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 and use the following functions:

1.

inverse:

A function that takes a string and makes all lowercase characters uppercase and all uppercase,

lowercase. Returns the string

2.

reverse:

A function that takes a string and returns a string in reverse order.

3.

toUpper:

A function that takes a string and makes all lowercase characters into uppercase. Returns a string

4.

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)

5.

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)

Explanation / Answer

#include<string>

#include<iostream>

using namespace std;

string inverse(string inp)

{

int i;

int len = inp.length();

for(i=0;i<len;i++)

{

if(inp[i]>=65 && inp[i]<=90)

{

inp[i] = 'a'+(inp[i]-65);

}

else if(inp[i]>=97 && inp[i]<=122)

{

inp[i] = 'A'+(inp[i]-97);

}

}

return inp;

}

string reverse(string inp)

{

int i;

int len= inp.length();

char temp;

for(i=0;i<len/2;i++)

{

temp = inp[i];

inp[i] = inp[len-i-1];

inp[len-i-1] = temp;

}

return inp;

}

string upperCase(string inp)

{

int i;

int len = inp.length();

for(i=0;i<len;i++)

{

if(inp[i]>=97 && inp[i]<=122)

{

inp[i] = 'A'+(inp[i]-97);

}

}

return inp;

}

int countWords(string inp)

{

int i=0;

int len = inp.length();

int word_len = 0;

int words_count = 0;

for(i=0;i<len;i++)

{

if(inp[i]==' ')

{

if(word_len>0)

{

words_count++;

word_len=0;

}

}

else

{

word_len++;

}

}

if(inp[i]=='' && inp[i-1]!=' ')

{

words_count++;

}

return words_count;

}

int countConsonents(string inp)

{

int i;

int len = inp.length();

int cons_count = 0;

for(i=0;i<len;i++)

{

if(((inp[i]>=65 && inp[i]<=90) ||(inp[i]>=97 && inp[i]<=122)) && inp[i]!='a' && inp[i]!='e' && inp[i]!='i' && inp[i]!='o' && inp[i]!='u' &&

inp[i]!='A' && inp[i]!='E' && inp[i]!='I' && inp[i]!='O' && inp[i]!='U' )

{

cons_count++;

}

}

return cons_count;

}

int main()

{

string inp;

cout<<"Enter Your String : ";

getline(cin,inp);

int choice = -1;

while(choice!=0)

{

cout<<"1) Inverse String "

<<"2) Reverse String "

<<"3) To Uppercase "

<<"4) Count Number Words "

<<"5) Count Consonants "

<<"6) Enter a Different String "

<<"7) Print the String "

<<"0) Quit ";

cout<<"Enter Your Choice : ";

cin>>choice;

switch(choice)

{

case 1:

inp = inverse(inp);

break;

case 2:

inp=reverse(inp);

break;

case 3:

inp = upperCase(inp);

break;

case 4:

cout<< countWords(inp)<<" ";

break;

case 5:

cout<< countConsonents(inp)<<" ";

break;

case 6:

getline(cin,inp);

getline(cin,inp);

break;

case 7:

cout<<inp<<" ";

break;

}

}

return 0;

}