fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. bool palindrome( const std::string &str );
  5.  
  6. int main()
  7. {
  8. std::string input;
  9.  
  10. while( std::getline( std::cin , input ) )
  11. {
  12. if( palindrome( input ) )
  13. std::cout << input << " is a palindrome." << std::endl;
  14. else
  15. std::cout << input << " is NOT a palindrome." << std::endl;
  16. }
  17. return 0;
  18. }
  19.  
  20. bool palindrome( const std::string &str )
  21. {
  22. bool isPalindrome = true;
  23. std::string::const_iterator left = str.cbegin() , right = str.cend()-1;
  24.  
  25. for( ; left < right && isPalindrome; ++left , --right )
  26. {
  27. if( *left != *right )
  28. isPalindrome = false;
  29. }
  30. return isPalindrome;
  31. }
Success #stdin #stdout 0s 3432KB
stdin
bob
fred
civic
banana
rotator
tattarrattat
giblit
stdout
bob is a palindrome.
fred is NOT a palindrome.
civic is a palindrome.
banana is NOT a palindrome.
rotator is a palindrome.
tattarrattat is a palindrome.
giblit is NOT a palindrome.