C Program to Check String is Palindrome using Stack

  • Write a program in C to check whether a String is Palindrome or not using Stack data structure.

We have to check whether a string is palindrome or not using stack. We can only use basic stack operations like push, pop, peek, isEmpty and isFull.
A string is palindrome, if string remains same after reversing the sequence of it's character. For example, "asdfdsa" is a palindrome string whereas string "mango" is not a palindrome string.

A stack is LAST IN FIRST OUT (LIFO) data structure. The element which is inserted last, is accessed first. Insertion and deletion of elements happens only at top of the Stack. The sequence of exit of elements from a stack is reverse of the sequence of their entry in stack.
Sequence of Entry.
A --> B --> C -- > D --> E
Sequence of Exit.
E --> D --> C --> B --> A
Algorithm to check palindrome string using stack
  • Find the length of the input string using strlen function and store it in a integer variable "length".
  • Using a for loop, traverse input string from index 0 to length-1 and push all characters in stack.
  • Remove (Pop) characters from stack one by one using a for loop and compare it with corresponding character of input string from beginning(traverse from index 0 to length-1). If we found a mismatch the input string is not a palindrome string otherwise palindrome string.

C program to check a string is palindrome or not using a stack

#include 
#include 

#define MAXSIZE 100
#define TRUE 1
#define FALSE 0 

// Structure defining Stack data structure
struct Stack {
    int top;
    int array[MAXSIZE];
} st;

/*
Initializes the top index to -1
*/
void initialize() {
 st.top = -1;
}

/*
 Checks if Stack is Full or not 
*/
int isFull() {   
    if(st.top >= MAXSIZE-1)
        return TRUE;
    else 
        return FALSE;
}

/*
 Checks if Stack is Empty or not
*/
int isEmpty() {
 if(st.top == -1)
     return TRUE;
 else 
     return FALSE;
}

/*
 Adds an element to stack and then increment top index 
*/
void push(int num) {
    if (isFull())
        printf("Stack is Full...\n");
    else {
        st.array[st.top + 1] = num;
        st.top++;
    }
}

/*
 Removes top element from stack and decrement top index
*/
int pop() {
    if (isEmpty())
        printf("Stack is Empty...\n");
    else {
     st.top = st.top - 1;
        return st.array[st.top+1];
    }
}

int main() {
    char inputString[100], c;
    int i, length;
    initialize();
    printf("Enter a string\n");
    gets(inputString);
    length = strlen(inputString);
    /* Push all characters of input String to Stack */
    for(i = 0; i < length; i++){
        push(inputString[i]);
    }
    /* Poping characters from stack returs the characters of input string
      in reverse order. We will then compare it with corresponding 
      characters of input string. If we found a mismatch the input 
      string is not a palindrome string */
    for(i = 0; i < length; i++){
        if(pop() != inputString[i]) {
            printf("Not a Palindrome String\n");
            return 0;
        }
    }

    printf("Palindrome String\n");
    return 0;
}
Output
Enter a string
ASDFGFDSA
Palindrome String
Enter a string
TECHCRASHCOURSE
Not a Palindrome String