Tuesday 7 September 2021

Valid Palindrome

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 not a palindrome.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
bool isPalindrome(char* s) {
   int i, j, len;
   len = strlen(s);
   i = 0; j = len - 1;
   while(i < j){
       // skip non alpha chars from start
       while(i < len && !isalnum(s[i])){
           i++;
       } 
       // skip non alpha chars from end
       while(j >= 0 && !isalnum(s[j])){
           j--;
       } 
       if(i < j && tolower(s[i]) != tolower(s[j])){
           return false;
       } 
       i++;
       j--;
   }
   return true;
}
Share:

1 comment:

  1. The people doing remote java developer jobs make an average of USD 72,809 per year; the US salary starts from $82,289 to $113,389+ turned to be the highest paying job.
    All you need to do is buckle up your skills, and the tips mentioned in a blog are going to be helpful in one's career. Thanks.

    ReplyDelete

Contact Me

Name

Email *

Message *

Popular Posts