-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpalindrome.c
74 lines (60 loc) · 1.11 KB
/
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
63
64
65
66
67
68
69
70
71
72
73
74
// C implementation of the approach
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* stack;
int top = -1;
// push function
void push(char ele)
{
stack[++top] = ele;
}
// pop function
char pop()
{
return stack[top--];
}
// Function that returns 1
// if str is a palindrome
int isPalindrome(char str[])
{
int length = strlen(str);
// Allocating the memory for the stack
stack = (char*)malloc(length * sizeof(char));
// Finding the mid
int i, mid = length / 2;
for (i = 0; i < mid; i++) {
push(str[i]);
}
// Checking if the length of the string
// is odd, if odd then neglect the
// middle character
if (length % 2 != 0) {
i++;
}
// While not the end of the string
while (str[i] != '\0') {
char ele = pop();
// If the characters differ then the
// given string is not a palindrome
if (ele != str[i])
return 0;
i++;
}
return 1;
}
// Driver code
int main()
{
char str[100];
printf("Enter a string: ");
scanf("%s",str);
if (isPalindrome(str)) {
printf("This is a palindrome\n");
}
else {
printf("This is not a palindrome\n");
}
return 0;
}