C You will write a short program that takes a string as inpu
C++
You will write a short program that takes a string as input, and asks the user if he/she wants to determine whether the string is a palindrome or reverse the string. A palindrome is a string of characters that are arranged in the same order when read from left to right or right to left.
http://www.fun-with-words.com/palin_explain.html
Create a string object to read the string from the user, #include . A user can enter a single word or a sentence. Therefore, you will need to use the getline() function to read the string from the user, i.e.std::getline(std::cin, sentence). Write a recursive function called determine_palindrome() that checks to see if the string is a palindrome.
If the user enters a string that is a palindrome, then you need to print, “You have entered a palindrome!!!”Otherwise, you will print, “This is not a palindrome.” You can either use the at() function to look at a single character or you can use brackets, [ ], to access individual characters, and you can get the length of a string by using size(). You need to ignore case and punctuation to determine if the string is a palindrome.
Create another recursive function called reverse_string() that takes a string of characters and reverses it. After reversing the string, print the new reversed string to the user.
Always ask the user if he/she wants to continue at the endJ
Example:
Do you want to reverse a string, check if it is a palindrome, or quit?
(Press 1 for reverse, 2 for palindrome, and anything else to quit) 1
Enter your string: Hello There
Reversed string: erehT olleH
Do you want to reverse a string, check if it is a palindrome, or quit?
(Press 1 for reverse, 2 for palindrome, and anything else to quit) 2
Enter your string: Bob
You have entered a palindrome!!!
Do you want to reverse a string, check if it is a palindrome, or quit? (Press 1 for reverse, 2 for palindrome, and anything else to quit) 0
Solution
#include<iostream>
 #include<stdlib.h>
 #include<stdio.h>
 #include<string.h>
 using namespace std;
 //Check for palindrome
 bool determine_palindrome( const std::string & word)
 {
 //for beginning
 std::string::const_iterator fwd = word.begin();
 //for end
 std::string::const_iterator rev = word.end();
if( rev - fwd <= 1 ) return true;
 if( *fwd++ != *--rev ) return false;
 //recursive call
 return determine_palindrome( std::string( fwd, rev ) );
 }
 //Reverse string
 void reverseStr(string word)
 {
 //calculates length
 int len = sizeof(word);
 //Loops length to 0 position
 while(len>=0)
 cout<<word[len--];
 }
 int main()
 {
 string sentence;
 int ch;
 //loops till other than 1 or 2 entered
 do
 {
 cout<<\"\ Do you want to reverse a string, check if it is a palindrome, or quit?\";
 cout<<\"\ (Press 1 for reverse, 2 for palindrome, and anything else to quit)\";
 fflush(stdin);
 //accepts choice
 cin>>ch;
 switch(ch)
 {
 case 1:
 cout<<\"\  Enter a string: \";
 fflush(stdin);
 std::getline(std::cin, sentence);
 reverseStr(sentence);
 break;
 case 2:
 cout<<\"\  Enter a string: \";
 fflush(stdin);
 std::getline(std::cin, sentence);
 if(determine_palindrome(sentence))
 cout<<\"You have entered a palindrome!!!\";
 else
 cout<<\"Not a palindrome!!!\";
 break;
 default:
 cout<<\"\  Wrong choice\";
 exit(0);
 }
 }while(1);
 }
Output:
Do you want to reverse a string, check if it is a palindrome, or quit?
 (Press 1 for reverse, 2 for palindrome, and anything else to quit)1
Enter a string: Demo
 omeD
 Do you want to reverse a string, check if it is a palindrome, or quit?
 (Press 1 for reverse, 2 for palindrome, and anything else to quit)2
Enter a string: demo
 Not a palindrome!!!
 Do you want to reverse a string, check if it is a palindrome, or quit?
 (Press 1 for reverse, 2 for palindrome, and anything else to quit)2
Enter a string: madam
 You have entered a palindrome!!!
 Do you want to reverse a string, check if it is a palindrome, or quit?
 (Press 1 for reverse, 2 for palindrome, and anything else to quit)2
Enter a string: Madam
 Not a palindrome!!!
 Do you want to reverse a string, check if it is a palindrome, or quit?
 (Press 1 for reverse, 2 for palindrome, and anything else to quit)0
Wrong choice



