Check whether the given string is Palindrome using Stack

Given a string str, the task is to find whether the given string is a palindrome or not using stack.

Examples:

Input: str = “geeksforgeeks”
Output: No

Input: str = “madam”
Output: Yes

Approach:



  • Find the length of the string say len. Now, find the mid as mid = len / 2.
  • Push all the elements till mid into the stack i.e. str[0…mid-1].
  • If the length of the string is odd then neglect the middle character.
  • Till the end of the string, keep popping elements from the stack and compare it with the current character i.e. string[i].
  • If there is mismatch then the string is not a palindrome. If all the elements match then the string is a palindrome.

Below is the implementation of the above approach:

C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C implementation of the approach
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
  
char* stack;
int top = -1;
  
// push function
void push(char ele)
{
    stack[++top] = ele;
}
  
// pop function
char pop()
{
    return stack[top--];
}
  
// Function that returns 1
// if str is a palindrome
int isPalindrome(char str[])
{
    int length = strlen(str);
  
    // Allocating the memory for the stack
    stack = (char*)malloc(length * sizeof(char));
  
    // Finding the mid
    int i, mid = length / 2;
  
    for (i = 0; i < mid; i++) {
        push(str[i]);
    }
  
    // Checking if the length of the string
    // is odd, if odd then neglect the
    // middle character
    if (length % 2 != 0) {
        i++;
    }
  
    // While not the end of the string
    while (str[i] != '\0') {
        char ele = pop();
  
        // If the characters differ then the
        // given string is not a palindrome
        if (ele != str[i])
            return 0;
        i++;
    }
  
    return 1;
}
  
// Driver code
int main()
{
    char str[] = "madam";
  
    if (isPalindrome(str)) {
        printf("Yes");
    }
    else {
        printf("No");
    }
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
static char []stack;
static int top = -1;
  
// push function
static void push(char ele)
{
    stack[++top] = ele;
}
  
// pop function
static char pop()
{
    return stack[top--];
}
  
// Function that returns 1
// if str is a palindrome
static int isPalindrome(char str[])
{
    int length = str.length;
  
    // Allocating the memory for the stack
    stack = new char[length * 4];
  
    // Finding the mid
    int i, mid = length / 2;
  
    for (i = 0; i < mid; i++) 
    {
        push(str[i]);
    }
  
    // Checking if the length of the String
    // is odd, if odd then neglect the
    // middle character
    if (length % 2 != 0
    {
        i++;
    }
  
    // While not the end of the String
    while (i < length) 
    {
        char ele = pop();
  
        // If the characters differ then the
        // given String is not a palindrome
        if (ele != str[i])
            return 0;
        i++;
    }
  
    return 1;
}
  
// Driver code
public static void main(String[] args)
{
    char str[] = "madam".toCharArray();
  
    if (isPalindrome(str) == 1
    {
        System.out.printf("Yes");
    }
    else 
    {
        System.out.printf("No");
    }
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
stack = []
top = -1
  
# push function
def push(ele: str):
    global top
    top += 1
    stack[top] = ele
  
# pop function
def pop():
    global top
    ele = stack[top]
    top -= 1
    return ele
  
# Function that returns 1
# if str is a palindrome
def isPalindrome(string: str) -> bool:
    global stack
    length = len(string)
  
    # Allocating the memory for the stack
    stack = ['0'] * (length + 1)
  
    # Finding the mid
    mid = length // 2
    i = 0
    while i < mid:
        push(string[i])
        i += 1
  
    # Checking if the length of the string
    # is odd, if odd then neglect the
    # middle character
    if length % 2 != 0:
        i += 1
  
    # While not the end of the string
    while i < length:
        ele = pop()
  
        # If the characters differ then the
        # given string is not a palindrome
        if ele != string[i]:
            return False
        i += 1
    return True
  
# Driver Code
if __name__ == "__main__":
  
    string = "madam"
  
    if isPalindrome(string):
        print("Yes")
    else:
        print("No")
  
# This code is contributed by
# sanjeev2552

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
  
static char []stack;
static int top = -1;
  
// push function
static void push(char ele)
{
    stack[++top] = ele;
}
  
// pop function
static char pop()
{
    return stack[top--];
}
  
// Function that returns 1
// if str is a palindrome
static int isPalindrome(char []str)
{
    int length = str.Length;
  
    // Allocating the memory for the stack
    stack = new char[length * 4];
  
    // Finding the mid
    int i, mid = length / 2;
  
    for (i = 0; i < mid; i++) 
    {
        push(str[i]);
    }
  
    // Checking if the length of the String
    // is odd, if odd then neglect the
    // middle character
    if (length % 2 != 0) 
    {
        i++;
    }
  
    // While not the end of the String
    while (i < length) 
    {
        char ele = pop();
  
        // If the characters differ then the
        // given String is not a palindrome
        if (ele != str[i])
            return 0;
        i++;
    }
    return 1;
}
  
// Driver code
public static void Main(String[] args)
{
    char []str = "madam".ToCharArray();
  
    if (isPalindrome(str) == 1) 
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

Yes

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.