Skip to content

Commit 63af633

Browse files
Valid palindrome
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent a576021 commit 63af633

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

125_valid_palindrome/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test valid_palindrome.c
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy