view palindrome/is_palindrome.cpp @ 7:e1578d3eee36

Added is_palindrome tests.
author Eris Caffee <discordia@eldalin.com>
date Tue, 05 May 2015 12:02:16 -0500
parents
children
line source
1 #include <cstring>
2 #include <cctype>
3 #include <iostream>
4 #include <string>
6 bool is_palindrome( const char *s ) {
7 const char * first = s;
8 const char * last = s + strlen(s) - 1;
9 while ( first < last ) {
10 while ( ! isalpha( *first ) && *first != 0 )
11 first++;
12 while ( ! isalpha( *last ) && last >= s )
13 last--;
14 if ( toupper( *first ) != toupper( *last ) ) {
15 return false;
16 }
17 first++;
18 last--;
19 }
20 return true;
21 }
23 bool is_palindrome( std::string s ) {
24 return is_palindrome( s.c_str() );
25 }
27 int main ( int argc, char **argv ) {
28 for ( int i = 1; i < argc; ++i ) {
29 std::string s(argv[1]);
30 std::cout << is_palindrome( s ) << std::endl;
31 }
33 return 0;
34 }