1、從兩端往中間code
bool isPalindrome( const char *s, int n){ if(*s==0x00 || n<1) return false; const char *head, *end; head=s; end=s+n-1; while( head < end ){ if( *head!=*end) return false; head++; end--; } return true; }
2、從中間至兩端class
bool isPalindrome( const char *s, int n){ if( *s==0x00 || n<1) return false; const char *tHead, *tEnd; if(n%2==0) { tHead=s+n/2-1; tEnd=s+n/2;} else { tHead=s+(n-1)/2; tEnd=s+(n-1)/2; } while( tHead >= s) { if( *tHead != *tEnd) return false; --tHead; ++tEnd; } return true; }