forked from bread-board-coding/Hacktober_Fest_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.c
63 lines (52 loc) · 885 Bytes
/
Stack.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
#include <stdio.h>
#include <stdlib.h>
struct stack
{
char *a;
int size;
int top;
};
int isempty(struct stack *st)
{
return st->top == -1;
}
void push(struct stack *st, char c)
{
st->top++;
st->a[st->top] = c;
}
char pop(struct stack *st)
{
char c = st->a[st->top];
st->top--;
return c;
}
int palindrome(char str[])
{
struct stack *st = (struct stack *)malloc(sizeof(struct stack));
st->a = (char *)malloc(100 * (sizeof(char)));
st->size = 100;
st->top = -1;
int i;
for ( i = 0; str[i] != '\0'; i++)
push(st, str[i]);
while (!isempty(st))
{
char c = pop(st);
if (c == str[i])
i++;
else
return 0;
}
return 1;
}
int main()
{
char str[100];
printf("Enter the string:");
scanf("%s", str);
if (palindrome(str))
printf("The given string is a palindrome!\n");
else
printf("The given string is not a palindrome!\n");
}