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
|
#include <algorithm>
#include <cctype>
#include <iostream>
#include <iterator>
#include <string>
bool is_palindrome(const std::string& phrase);
bool non_white(const char& ch);
char lowercase(char& ch);
void waitForEnter();
int main()
{
std::string input = "Lorem ipsum dolor sit amet, an vim viris animal "
"dolorum, in mei legere vocent";
if (is_palindrome(input)) { std::cout << "Palindrome!!\n"; }
else { std::cout << "Not Palindrome!\n"; }
input = "In girum imus nocte et consumimur igni";
if (is_palindrome(input)) { std::cout << "Palindrome!!\n"; }
else { std::cout << "Not Palindrome!\n"; }
waitForEnter();
return 0;
}
bool is_palindrome(const std::string& phrase)
{
std::string temp(phrase.length(), ' ');
std::remove_copy_if(phrase.begin(), phrase.end(), temp.begin(), non_white);
std::cout << "temp: \"" << temp << "\"\n";
temp.erase(std::remove_if(temp.begin(), temp.end(), ::isspace),
temp.end());
std::transform(temp.begin(), temp.end(), temp.begin(), lowercase);
std::cout << "temp: \"" << temp << "\"\n";
return equal(temp.begin(), temp.end(), temp.rbegin());
}
bool non_white(const char& ch)
{
if (ch == ' ') { return true; }
return false;
}
char lowercase(char& ch)
{
if (int(ch) <= 90 && int(ch) >= 65) {
return char(int(ch) + 32);
}
return ch;
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
| |