1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <math.h>
#include <algorithm>
#include <string>
using namespace std;
string toUpper(string s1);
string removeNonAlphabets(string s1);
bool isPalindrome(string s1, int begins, int ends);
string toUpper(string s1)
{
std::transform(s1.begin(), s1.end(), s1.begin(), ::toupper);
return s1;
}
string removeNonAlphabets(string s1)
{
for(string::iterator i = s1.begin(); i != s1.end(); i++)
{
if(!isalpha(s1.at(i - s1.begin())))
s1.erase(i);
}
return s1;
}
bool isPalindrome(string s1, int begins, int ends)
{
if(begins == ends)
return true;
else if(begins<ends)
{
if(s1[begins]==s1[ends])
return isPalindrome(s1,begins+1,ends-1);
else
return false;
}
return false;
}
int main()
{
string s1;
cout << "Enter a word, phrase or sentence to see if it's a palindrome: " << endl;
getline(cin,s1);
cout << endl << "The original input string contains: " << endl;
cout << s1 << endl;
cout << endl << "After conversion to upper-case, input string becomes: " << endl;
s1 = toUpper(s1);
cout << s1 << endl;
cout << endl << "After removal of non-alphabets, input string becomes: " << endl;
s1 = removeNonAlphabets(s1);
cout << s1 << endl;
if(isPalindrome(s1,0,s1.size()-1))
{
cout << endl << s1 << " IS a palindrome!" << endl;
}
else
{
cout << endl << s1 << " is NOT a palindrome!" << endl;
}
return 0;
}
|