MIT-Curricular/DS/C/Lab/Week3/StackPalindrome.c

47 lines
740 B
C
Raw Normal View History

2024-08-05 01:10:12 +05:30
#include <stdio.h>
#define MAX_SIZE 100
int top = -1;
char stack[MAX_SIZE];
void push(char c) {
if (top < MAX_SIZE - 1) {
stack[++top] = c;
}
}
char pop() {
if (top >= 0) {
return stack[top--];
}
return '\0';
}
int is_palindrome(char *str) {
int i;
for (i = 0; str[i] != '\0'; i++) {
push(str[i]);
}
for (i = 0; str[i] != '\0'; i++) {
if (str[i] != pop()) {
return 0;
}
}
return 1;
}
int main() {
char str[MAX_SIZE];
printf("Enter a string: ");
scanf("%s", str);
if (is_palindrome(str)) {
printf("%s is a palindrome.\n", str);
} else {
printf("%s is not a palindrome.\n", str);
}
return 0;
}