File tree Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ all :
2+ gcc -O2 -o test valid_palindrome.c
Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+ #include <stdlib.h>
3+ #include <stdbool.h>
4+ #include <string.h>
5+
6+ static bool valid (char c )
7+ {
8+ return (c >= '0' && c <= '9' ) || (c >= 'a' && c <= 'z' ) || (c >= 'A' && c <= 'Z' );
9+ }
10+
11+ bool isPalindrome (char * s )
12+ {
13+ int len = strlen (s );
14+ int low = 0 ;
15+ int high = len - 1 ;
16+
17+ while (low < high ) {
18+ if (!valid (s [low ])) {
19+ low ++ ;
20+ } else if (!valid (s [high ])) {
21+ high -- ;
22+ } else if (s [low ] == s [high ]) {
23+ low ++ ;
24+ high -- ;
25+ } else {
26+ if (isalpha (s [low ]) && isalpha (s [high ])) {
27+ int diff = s [low ] > s [high ] ? s [low ] - s [high ] : s [high ] - s [low ];
28+ if (diff == 'a' - 'A' ) {
29+ low ++ ;
30+ high -- ;
31+ } else {
32+ return false;
33+ }
34+ } else {
35+ return false;
36+ }
37+ }
38+ }
39+
40+ return low >= high ;
41+ }
42+
43+ int main (int argc , char * * argv )
44+ {
45+ if (argc != 2 ) {
46+ fprintf (stderr , "Usage: ./test string\n" );
47+ exit (-1 );
48+ }
49+ printf ("%s\n" , isPalindrome (argv [1 ]) ? "true" : "false" );
50+ return 0 ;
51+ }
You can’t perform that action at this time.
0 commit comments