Sunday, 17 July 2016

Palindrome String

/*    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
    Example:
    "A man, a plan, a canal: Panama" is a palindrome.
    "race a car" is not a palindrome.
    Return 0 / 1 ( 0 for false, 1 for true ) for this problem
*/

 #include<iostream>  
 #include<string.h>  
 using namespace std;  
 int isPalindrome(char *s){  
   int i, j, len;  
   len = strlen(s);  
   i = 0; j = len - 1;  
   while(i < j){  
     while(i < len && !isalnum(s[i])){  
       i++;  
     }  
     while(!isalnum(s[j])){  
       j--;  
     }  
     if(i < j && tolower(s[i]) != tolower(s[j])){  
       return 0;  
     }else if(i < j && tolower(s[i]) == tolower(s[j])){  
       i++;  
       j--;  
     }  
   }  
   return 1;  
 }  
 int main(){  
   char *s = "a";  
   cout << isPalindrome(s);  
   return 0;  
 }  
Share:

0 comments:

Post a Comment

Contact Me

Name

Email *

Message *

Popular Posts

Blog Archive