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 !
Short Summary:
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:

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