Answer to Question #297552 in C++ for Rival

Question #297552

Objective:

Write a program that accepts a string as an input and perform following –

Write a function that accepts a string as an argument and display the reverse of that string and 

return that string.

Write another function that accepts a string as its argument and check whether the string is 

palindrome or not.

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.


1
Expert's answer
2022-02-14T12:04:41-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() {
	//1. The user is asked to enter a string.
	string inputString;
	cout<<"Enter a String: ";
	getline(cin,inputString);
	//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
	char option=' ';
	do{
		cout<<"A) Display the reverse of the string\n";
		cout<<"B) Check the string is palindrome or not\n";
		cout<<"C) Exit the program\n";
		cout<<"Your choice: ";
		cin>>option;
		if(option=='A' || option=='a'){
			cout<<"reverse string: "<<reverse(inputString)<<"\n";
		}else if(option=='B' || option=='b'){
			if(isPalindrome(inputString)){
				cout<<"The string is palindrome\n";
			}else{
				cout<<"The string is NOT palindrome\n";
			}
		}else if(option=='C' || option=='c'){
			//exit
		}else{
			cout<<"Wrong menu item\n\n";
		}
		//3. The program performs the operation selected by the user and repeats until the user  selects E to exit the program.
	}while(option!='C' && option!='c');


	
	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