#include <iostream>
#include <string>
template < class ite>
ite SkipNotAlpha( ite it, ite end) {
while ( it ! = end) {
if ( isalpha ( * it) ! = 0 ) break ;
it++ ;
}
return it;
}
bool Is_Palindrome( std:: string & str) {
auto ita = str.begin ( ) ;
auto itb = str.rbegin ( ) ;
while ( 1 ) {
ita = SkipNotAlpha( ita, str.end ( ) ) ;
itb = SkipNotAlpha( itb, str.rend ( ) ) ;
if ( tolower ( * ita) ! = tolower ( * itb) ) return false ;
ita++ ;
itb++ ;
if ( ita == str.end ( ) ) {
ita = SkipNotAlpha( ita, str.end ( ) ) ;
itb = SkipNotAlpha( itb, str.rend ( ) ) ;
break ;
}
if ( itb == str.rend ( ) ) {
ita = SkipNotAlpha( ita, str.end ( ) ) ;
itb = SkipNotAlpha( itb, str.rend ( ) ) ;
break ;
}
}
return ( ita == str.end ( ) ) && ( itb == str.rend ( ) ) ;
}
void check_palindrome( std:: string str) {
std:: cout << str << ( Is_Palindrome( str) ? " is" : " is NOT" ) << " a palindrome." << std:: endl ;
}
int main( ) {
check_palindrome( "boo" ) ;
check_palindrome( "Debug gubeD" ) ;
check_palindrome( "Debug gubeD!" ) ;
check_palindrome( "A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal-Panama!" ) ;
check_palindrome( "A tin mug for a jar of gum, Nita." ) ;
check_palindrome( "A Toyota! Race fast, safe car! A Toyota!" ) ;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgoKdGVtcGxhdGU8Y2xhc3MgaXRlPgppdGUgU2tpcE5vdEFscGhhKGl0ZSBpdCwgaXRlIGVuZCl7Cgl3aGlsZSAoaXQgIT0gZW5kKXsKCQlpZiAoaXNhbHBoYSgqaXQpIT0wKSBicmVhazsKCQlpdCsrOwoJfQoJcmV0dXJuIGl0Owp9Cgpib29sIElzX1BhbGluZHJvbWUoc3RkOjpzdHJpbmcmIHN0cil7CglhdXRvIGl0YSA9IHN0ci5iZWdpbigpOwoJYXV0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YW5hbWEhIik7CgljaGVja19wYWxpbmRyb21lKCJBIHRpbiBtdWcgZm9yIGEgamFyIG9mIGd1bSwgTml0YS4iKTsKCWNoZWNrX3BhbGluZHJvbWUoIkEgVG95b3RhISBSYWNlIGZhc3QsIHNhZmUgY2FyISBBIFRveW90YSEiKTsKCglyZXR1cm4gMDsKfQ==
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.