/
string_palindrome.c
59 lines (43 loc) · 960 Bytes
/
string_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
#include <stdio.h>
#include <string.h>
void isPalindrome(char str[]){
// 0 if not palindrome
// 1 if palindrome
//create reverse string
int len = strlen(str);
char reversed[len+1];
for (int i = len-1, j =0 ; i >= 0 ; i--, j++){
reversed[j] = str[i];
}
reversed[len] = '\0';
//compare them
int result = strcmp(str, reversed);
if (result == 0){
printf("String is PALINDROME");
}else{
printf("STRING IS NOT PALINDROME");
}
}
void isPalindrome2(char str[]){
int left = 0;
int right = strlen(str) -1;
while (left < right){
if (str[left] != str[right]){
printf("STRING IS NOT PALINDROME");
return;
}
left++;
right--;
}
printf("STRING IS PALINDROME");
}
int main(void) {
isPalindrome("ABCDCBA");
printf("\n");
isPalindrome("Palindrome");
printf("\n");
isPalindrome2("Palindrome");
printf("\n");
isPalindrome2("ABCDCBA");
return 0;
}