-
Notifications
You must be signed in to change notification settings - Fork 0
/
palindrome.c
62 lines (53 loc) · 905 Bytes
/
palindrome.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char *strrev(char *str)
{
char *p1, *p2;
if (! str || ! *str)
return str;
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}
void strlwr(char *str)
{
char p = str[0];
int i=0;
while(p!='\0')
{
str[i]=(char)tolower(p);
i++;
p=str[i];
}
}
int is_palindrome(char *str)
{
char *strLwrRev;
strLwrRev = (char*)malloc(strlen(str));
strlwr(str);
strcpy(strLwrRev, str);
strLwrRev = strrev(strLwrRev);
if (!strcmp(strLwrRev, str))
{
return 1;
}
else
{
return 0;
}
}
#ifndef RunTests
int main(int argc, char *argv[])
{
char *str;
str = (char*)malloc(strlen(argv[1]));
strcpy(str, argv[1]);
printf("%d\n", is_palindrome(str));
}
#endif