Value Palindrome

来源:互联网 发布:php公司网站源码 编辑:程序博客网 时间:2024/06/06 08:34
运用了STL函数。#include<iostream>#include<algorithm>using namespace std;bool isPalindrome1(string text){text.erase(remove_if(text.begin(), text.end(),[](char c){//lambdareturn !isalpha(c); }), text.end());transform(text.begin(), text.end(), text.begin(), tolower);return equal(text.begin(), text.begin() + text.size() / 2, text.rbegin());}bool isPalindrome2(string text){transform(text.begin(), text.end(), text.begin(), tolower);auto left = text.begin();auto right = prev(text.end());while (left < right){if (!::isalpha(*left))  ++left;else if (!::isalpha(*right))  --right;else if (*left != *right) return false;++left;--right;}return true;}int main(){string text = "abcD48dC*bA)";if (isPalindrome2(text))cout << "hello";elsecout << "wong";}


0 0