In: Computer Science
c++:
I need #include <iostream> header
A palindrome (Links to an external site.) is a word, phrase, number, or sequence of words that reads the same backward as forward. Punctuation, capitalization, and spaces between the words or lettering are allowed.
Here are some examples of word and phrase palindromes.
Words:
Civic
Kayak
Level
Madam
Mom
Noon
Racecar
Radar
Refer
Rotor
Phrases:
Don't nod.
I did, did I?
My gym
Step on no pets
Top spot
Eva, can I see bees in a cave?
No lemon, no melon
Your assignment is to create a function that tests whether a string is a palindrome or not.
bool isPalindrome(string phrase)
Your main function should consist of a while loop that calls isPalindrome() until the user exits the program with a Control-D or other interrupt indicator. Your output should prompt for a candidate palindrome and reprint the phrase and let the user know whether the phrase meets the criteria or not. Be sure your output demonstrates various test cases including invalid input.
Hints:
You may want to remove spaces, punctuation, and spaces, then reverse the order of the phrase you are testing, then compare the reversed phrase with the original, to see if it matches.
regex regexp("[^a-zA-z]"); string result = regex_replace(phrase, regexp,""); // remove spaces, punctuation, etc.
2. You should change the phrase to lower case before you do your comparison. There are several ways to do this. I used the transform, and ::tolower() to accomplish this. You can read up on this option.
3. The standard library has a reverse function that you can use out of the box, but it reverses the original string. Therefore, before reversing you will want to make a copy of the original and compare the copy to the original. You can use copy() for that purpose. Again, look up and study this function.
4. There is a method in the string library to compare strings to see if two strings are equal or not. Read up on compare() too.
5. You can optionally use shorthand conditional syntax to print output "true" or "false" in your test results as follows:
(isPalindrome("Sit on a potato pan, Otis") ? "true" : "false")
Code:
#include <iostream>
#include<string.h>
using namespace std;
bool isPalindrome(string str)
{
int l=0,h=str.length();
for (int i=0;i<h;i++) // convert to lowercase string
str[i] = tolower(str[i]);
while (l <= h) {
if (!(str[l] >= 'a' && str[l] <= 'z')) // remove other symbol in left of string
l++;
else if (!(str[h] >= 'a' && str[h] <= 'z')) //remove other symbols in right of string
h--;
else if (str[l] == str[h]) // If characters are equal
l++, h--;
else
return false; // If characters are not equal then not palindrome
}
return true; // Returns true if palindrome
}
int main()
{
string str;
char ch;
while(1)
{
str.clear();
fflush(stdin);
cout<<"Enter string: ";
getline (cin, str);
if (isPalindrome(str))
cout <<"The string is palindrome\n";
else
cout << "The string is not palindrome\n";
cout<<"Do you want to enter another string?(y/n)\n";
cin>>ch;
if(ch!='y')
exit(0);
}
return 0;
}
Output: