-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_palindrome.c
86 lines (76 loc) · 1.24 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 50
struct stack
{
int a[max];
int top;
};
void initialize(struct stack *s)
{
s->top = -1;
}
void push(struct stack *s, int element)
{
if(s->top == (max-1))
{
printf("Stack Overflow");
return;
}
s->top++;
s->a[s->top] = element;
}
int pop(struct stack *s)
{
if(s->top == -1)
{
printf("Stack Underflow");
return -1;
}
int x=s->a[s->top];
s->top--;
return x;
}
int peep(struct stack *s)
{
if(s->top == -1)
{
printf("Stack Empty");
return -1;
}
int x=s->a[s->top];
return x;
}
void palindrome(struct stack *s, char *word)
{
int i;
for(i=0; word[i] != '\0'; i++)
{
push(s, word[i]);
}
char y[50];
int j=0;
while (s->top != -1)
{
y[j] = pop(s);
j++;
}
y[j] = '\0';
if (strcmp(word, y) == 0)
{
printf("String is a palindrome \n");
return;
}
printf("String is not a palindrome \n");
}
int main()
{
struct stack *s=(struct stack*)malloc(sizeof(struct stack));
initialize(s);
char word[50];
printf("Enter a string: ");
scanf("%s", word);
palindrome(s, word);
return 0;
}