Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #define TYPE char
- #define STACKSIZE 100
- typedef struct Stack {
- int top;
- TYPE items[STACKSIZE];
- }Stack;
- void initialize(Stack *s){
- s->top=0;
- }
- void push(Stack *s, TYPE value){
- s->items[s->top]=value;
- s->top++;
- }
- TYPE pop(Stack *s){
- s->top--;
- return s->items[s->top];
- }
- int isFull(Stack *s){
- return s->top==STACKSIZE;
- }
- int isEmpty(Stack *s){
- return s->top == 0;
- }
- Stack reverseStack(Stack s){ /* Function to reverse the stack to put it in another stack */
- Stack n; initialize(&n);
- while(!isEmpty(&s)) push(&n,pop(&s));
- return n;
- }
- int main(){
- system("clear");
- // Declaring Stack with name S & initialize it
- Stack s; initialize(&s);
- Stack n; initialize(&n);
- char x[STACKSIZE]; printf("Enter word to test if palindrome : "); gets(x);
- int i; for(i=0;x[i];i++) push(&s,x[i]);
- n = reverseStack(s);
- int flag=1;
- while(!isEmpty(&s))
- if(pop(&s) != pop(&n)) {
- flag = 0; break;
- }
- printf("%s is",x); flag ? printf(" ") : printf(" NOT "); printf("palindrome.\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment