how to use push and pop here???

thanks!

#include<stdio.h>
#include<stdlib.h>

void push(char palindrome[20], int *top, int value)
{
    if(*top<=10)
    {
        palindrome[*top]=value;
        *top++;
    }
    else
    {
        printf("Stack is full!!");
        exit(0);
    }

}

int value;
void pop(char palindrome[10], int *top)
{
    if(*top>=0)
    {
        value=palindrome[*top];
        *top=*top-1;
    }
    else
    {
        printf("The stack is empty!!\n");
    }
}

char palindrome[10];
int top = -1;
int value;
int x;

void main()
{
    clrscr();
    printf("Enter word: ");
    scanf("%s",&value);

    for(top=0;palindrome[top]!=x;top++)
    {
    }
    top--;

    for(value=0;palindrome[value]!=x;value++,top--)
    {
        if(palindrome[top]!=palindrome[value])
        x=1;
    }
    if(x==0)
    {
        printf("The word you've entered is a palindrome!!\n");
    }
    else
    {
        printf("The word you've entered is not a palindrome!!\n");
    }
    getch();
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You never initialized the value of x. When is the loop supposed to stop? Stopping at the null character '\0' might be a good idea... Also, you can't have both the loop and and function keeping track of top. You can use a while loop to go through the string and push will modify top for you.

Look abby2589 ,

I didn't read all your code but from the general idea of the code i assume that you want to know how to use the stack to check if the word is palindrome or not.

- After you enter the string you will push all of its element from the first character till the end of the string (Using a Pointer ofcourse till the Pointer Reaches NULL).

-Then Pop all the elements from the stack and recieve them in another string.

-And compare the newly created string to the original string that been entered at the first of your program.

This article has been dead for over six months. Start a new discussion instead.