Gadgets 4 Students Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
1 like 0 dislike
5.3k views
in ASTU B.Tech (CSE-III Sem) Data Structure and Algorithms Lab by Goeduhub's Expert (7.6k points)
Write a program to implement stack can be used to check whether a string is a palindrome or not.

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
selected by
 
Best answer

Write a program to implement stack can be used to check whether a string is a palindrome or not

A palindrome is a word, phrase, number or sequence of words that reads the same backward as forward. Punctuation and spaces between the words or lettering is allowed.

Example - Malayalam , nurses run , anna , Amma etc

Program

Take a string as input and store it in the array s[].
Load each character of the array s[] into the array stack[].
Use variables front and top to represent the last and top element of the array stack[].
Using for loop compare the top and last element of the array stack[]. If they are equal, then delete the top element, increment the variable front by 1 and compare again.
If they are not equal, then print the output as “It is not a palindrome”.
Compare the elements in the steps 4-5 upto the middle element of the array stack[].
If every characters of the array is equal, then it is a paliandrome.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX 50

int top = -1, front = 0;

int stack[MAX];

void push(char);

void pop();

void main()

{

    int i, choice;

    char s[MAX], b;

    while (1)

    {

        printf("1-enter string\n2-exit\n");

        printf("enter your choice\n");

        scanf("%d", &choice);

        switch (choice)

        {

        case 1:

            printf("Enter the String\n");

            scanf("%s", s);

            for (i = 0;s[i] != '\0';i++)

            {

                b = s[i];

                push(b);

            }

            for (i = 0;i < (strlen(s) / 2);i++)

            {

                if (stack[top] == stack[front])

                {

                    pop();

                    front++;

                }

                else

                {

                    printf("%s is not a palindrome\n", s);

                    break; 

                }

            }

            if ((strlen(s) / 2)  ==  front)

                printf("%s is palindrome\n",  s);

            front  =  0;

            top  =  -1;

            break;

        case 2:

            exit(0);

        default:

            printf("enter correct choice\n");

        }

    }

}

void push(char a)

{

    top++;

    stack[top]  =  a;

}

void pop()

{

    top--;

}

Output

output


For more Assam Science and Technology University(ASTU) CSE-III Sem Data Structure and Algorithms Lab Experiments Click here


 

Best Online Learning Opportunities

UDEMY::  Attend All Udemy Courses in Just INR 450[Coupon]
Coursera:: Join For FREE
UDACITY (Best Nanodegrees)::

Machine Learning Engineer || Artificial Intelligence
Data Scientist

3.3k questions

7.1k answers

395 comments

4.5k users

Related questions

0 like 0 dislike
1 answer 1.1k views

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory
...