Question

Option 3 – String Reverse When the user chooses option 3, a new function will be...

Option 3 – String Reverse

When the user chooses option 3, a new function will be called.
This function will read a string from the user. This string may or may not have spaces in it.
This function will call a recursive function that prints the string in reverse order.   You decide what should be passed to this function, but it must be recursive.

void mathBreak()
{

string str;
int subOption;
do//Sentinal controlled loop for the math break menu
  
{
//Displaying the menu
   cout << "\n\n\n Math Break Menu "<<endl;
cout << "******************"<<endl;
cout << endl << "1. Even or Odd" << endl;
cout << "2. Prime Number" << endl;
cout << "3. String Reverse" << endl;
cout << "4. End my Math Break" << endl;
cout << "Enter your choice: ";
cin>> subOption;
//Error checking
while (subOption< 0 || subOption >4)
{
//Error message
cout <<endl<<endl;
cout <<"Invalid Choice. Please Choose Again. ";
cin >> subOption;
}
//Selection 1 sends user to even/odd function
if (subOption==1)
evenOdd();
//Selection 2 sends user to prime number function
else if (subOption==2)
primeNumber();
  
}while(subOption!=3);//Post check test for selection !

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Short Summary:

  • Used a recursive function to reverse string. it prints the string in reverse order character by character.
  • Wrote function for EvenOdd and PrimeNumbers as well
  • Shown sample output for string with space and without space

Source Code:

#include <iostream>
using namespace std;

//Even or odd function
void evenOdd()
{
//get positive number
int numberInput;
cout << "Enter a number: ";
cin >> numberInput;
  
//use modulus to find even or odd
if (numberInput % 2 == 0)
{
cout << numberInput << " is Even number";
}
else
{
cout << numberInput << " is Odd number";
}
}

//Prime number function
void primeNumber()
{
//Get a user input
int numberInput;
cout << "Enter a number: ";
cin >> numberInput;
bool isPrime = true;
//Prime numbers divided by 1 and themselves only
for(int i = 2; i <= numberInput / 2; ++i)
{
if(numberInput % i == 0)
{
isPrime = false;
break;
}
}
  
//Print the result
if (isPrime)
cout << numberInput <<" is a prime number";
else
cout << numberInput <<" is not a prime number";
}
  
/* Recursive function that prints the string in reverse order */
void
reverseString(string strInput)
{
//if size is zero, dont call recursive or print.. move to next
if(strInput.length() == 0)
{
return;
}
reverseString(strInput.substr(1));
//print one char at atime
cout << strInput[0];
}


void mathBreak()
{
//string str;
int subOption;
do//Sentinal controlled loop for the math break menu
{
//Displaying the menu
cout << "\n\n\n Math Break Menu "<<endl;
cout << "******************"<<endl;
cout << endl << "1. Even or Odd" << endl;
cout << "2. Prime Number" << endl;
cout << "3. String Reverse" << endl;
cout << "4. End my Math Break" << endl;
cout << "Enter your choice: ";
  
cin>> subOption;
//Error checking
while (subOption <= 0 || subOption >4)
{
//Error message
cout <<endl<<endl;
cout <<"Invalid Choice. Please Choose Again. ";
cin >> subOption;
}
//Selection 1 sends user to even/odd function
if (subOption == 1)
evenOdd();
//Selection 2 sends user to prime number function
else if (subOption == 2)
primeNumber();
//Selection 3 sends user to string reverse function
else if (subOption == 3)
{
string str;
  
cout << "Please enter your string: \n";
cin.ignore();
getline(cin, str);
  
//Call recursive function to reverse the string
cout << "Reversed string: ";
reverseString(str);
}

//Selection 4 Quit
else if (subOption == 4)
{
cout << "Good Bye! See you soon!";
}
}while(subOption != 4 );//Post check test for selection !
}

int main()
{
mathBreak();
return 0;
}

Sample Output:

Add a comment
Know the answer?
Add Answer to:
Option 3 – String Reverse When the user chooses option 3, a new function will be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT