Write a ‘C’ program which accepts the string and check whether the string is Palindrome or not using stack. (Use Static / Dynamic implementation of Stack).
#include <stdio.h>
#include <string.h>
#define MAX 100
// Stack implementation using array
char stack[MAX];
int top = -1;
// Push function
void push(char ch) {
if (top == MAX - 1) {
printf("Stack Overflow\n");
return;
}
stack[++top] = ch;
}
// Pop function
char pop() {
if (top == -1) {
printf("Stack Underflow\n");
return '\0';
}
return stack[top--];
}
int main() {
char str[MAX];
int len, i, flag = 1;
printf("Enter a string: ");
scanf("%s", str);
len = strlen(str);
// Push all characters of the string into stack
for(i = 0; i < len; i++) {
push(str[i]);
}
// Pop characters and compare with original string
for(i = 0; i < len; i++) {
if(str[i] != pop()) {
flag = 0;
break;
}
}
if(flag)
printf("The string \"%s\" is a palindrome.\n", str);
else
printf("The string \"%s\" is NOT a palindrome.\n", str);
return 0;
}
