Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a Canal:panama" is a palindrome.
"Race a Car" is a palindrome.
Note:
Are you consider that the string might is empty? This was a good question to ask during a interview.
For the purpose of this problem, we define empty string as valid palindrome.
Title: Determine if a string is a string of words back
Idea: Two pointers from the string's head and tail to the middle, only to encounter is the word characters comparison is equal, any one of the pointers encountered other symbols continue to walk until the character or the character boundaries.
Attention:
1, empty characters are also palindrome
2, must be the boundary to judge, otherwise when the string only special symbol without the letter will be out of bounds, in VS2012 incredibly cross-border program did not crash, Linux also did not.
3. Case insensitive
4, when strlen (s) < 2 must be a palindrome
#include <stdlib.h>#include <stdio.h>#include <math.h>#include <string.h>#include <stdbool.h>#define ISSTR (a) ((a>= ' a ' &&a<= ' z ') | | (a>= ' A ' &&a<= ' Z ') | | (a>= ' 0 ' &&a<= ' 9 '))BOOLIspalindrome (Char* s) {if(NULL = = s)return true;if(' + '= = s)return true;if(strlen(s) <2)return true;Char* PA = s;Char* PB = S;Char* L = s;//border of PA Point while(*PB! =' + ') pb++; pb--;//make PB point the last Character,nor '! Char* n = PB;//border of PB Point while(PA < PB) { while(!isstr (*PA) && pa<=n) pa++; while(!isstr (*PB) && pb>=l) pb--;if((*pa! = *PB) && (ABS(*PA-*PB)! =' A '-' A ') && (Isstr (*PA)) && (Isstr (*PB)))return false;Else{pa++; pb--; } }return true;}intMain () {Char* s ="A man, a plan, a Canal:panama";BOOLR = Ispalindrome (s);printf("s is ispalindrome?:%d \ n", R);Char*S1 ="";BOOLR1 = Ispalindrome (S1);printf("S1 is Ispalindrome?:%d \ n", R1);Char*S2 ="*.";BOOLr2 = ispalindrome (s2);printf("S2 is ispalindrome?:%d \ n", r2);Char*S3 ="Sore was I ere I saw Eros.";BOOLR3 = Ispalindrome (S3);printf("S3 is Ispalindrome?:%d \ n", R3);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Leetcode]-valid palindrome