fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. template<class ite>
  5. ite SkipNotAlpha(ite it, ite end){
  6. while (it != end){
  7. if (isalpha(*it)!=0) break;
  8. it++;
  9. }
  10. return it;
  11. }
  12.  
  13. bool Is_Palindrome(std::string& str){
  14. auto ita = str.begin();
  15. auto itb = str.rbegin();
  16. while (1){
  17. ita = SkipNotAlpha(ita, str.end());
  18. itb = SkipNotAlpha(itb, str.rend());
  19. if (tolower(*ita) != tolower(*itb)) return false;
  20. ita++;
  21. itb++;
  22. if (ita == str.end()){
  23. ita = SkipNotAlpha(ita, str.end());
  24. itb = SkipNotAlpha(itb, str.rend());
  25. break;
  26. }
  27. if (itb == str.rend()){
  28. ita = SkipNotAlpha(ita, str.end());
  29. itb = SkipNotAlpha(itb, str.rend());
  30. break;
  31. }
  32.  
  33. }
  34. return (ita == str.end()) && (itb == str.rend());
  35. }
  36.  
  37. void check_palindrome(std::string str) {
  38. std::cout << str << (Is_Palindrome(str) ? " is" : " is NOT") << " a palindrome." << std::endl;
  39. }
  40. int main() {
  41. check_palindrome("boo");
  42. check_palindrome("Debug gubeD");
  43. check_palindrome("Debug gubeD!");
  44. check_palindrome("A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal-Panama!");
  45. check_palindrome("A tin mug for a jar of gum, Nita.");
  46. check_palindrome("A Toyota! Race fast, safe car! A Toyota!");
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
boo is NOT a palindrome.
Debug gubeD is a palindrome.
Debug gubeD! is a palindrome.
A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal-Panama! is a palindrome.
A tin mug for a jar of gum, Nita. is a palindrome.
A Toyota! Race fast, safe car! A Toyota! is a palindrome.