Answer to Question #297581 in C++ for Rival

Question #297581


Description: 

A palindrome is a string, which when read in both forward and backward ways is the same. 

Example: lol, pop, radar, madam, etc. 

To check if a string is a palindrome or not, a string needs to be compared with the reverse of  

itself. 

Demonstrate these two functions in a program that performs the following steps: 

1. The user is asked to enter a string. 

2. The program displays the following menu: 

A) Display the reverse of the string 

B) Check the string is palindrome or not 

C) Exit the program 

3. The program performs the operation selected by the user and repeats until the user  

selects E to exit the program. 

Output: 

Enter a string : madam 

Please enter a choice according to menu: 

A) Display the reverse of the string 

B) Check the string is palindrome or not 

C) Exit the program 

Please enter choice: A

The reverse of the string is : madam 

Please enter choice: B 

Madam is palindrome 

Please enter choice: E 

Terminate the program


1
Expert's answer
2022-02-14T12:04:45-0500


#include <iostream>
#include <string>
using namespace std;


string reverse(string inputString){
	string reverseString="";
	for(int j=inputString.length()-1;j>=0;j--){
		reverseString+=inputString[j];
	}
	return reverseString;
}


bool isPalindrome(string inputString){
	return inputString.compare(reverse(inputString))==0; 
}


int main() {
	string inputString;
	cout<<"Enter a String: ";
	getline(cin,inputString);
	char option=' ';
	do{
		cout<<"Please enter a choice according to menu:\n";
		cout<<"A) Display the reverse of the string\n";
		cout<<"B) Check the string is palindrome or not\n";
		cout<<"E) Exit the program\n";
		cout<<"Please enter choice: ";
		cin>>option;
		if(option=='A' || option=='a'){
			cout<<"The reverse of the string is: "<<reverse(inputString)<<"\n";
		}else if(option=='B' || option=='b'){
			if(isPalindrome(inputString)){
				cout<<inputString<<" is palindrome\n";
			}else{
				cout<<inputString<<" is NOT palindrome\n";
			}
		}else if(option=='E' || option=='e'){
			//exit
		}else{
			cout<<"Wrong menu item\n\n";
		}
	}while(option!='E' && option!='e');
	cout<<"Terminate the program\n\n";
	
	system("pause");
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog