check whether a equation is palindrome number or not in c programming
February 26, 2023

Check the Palindrome number program in c programming

In this tutorial, we are going to write a program in a c programming language that will get the string as input from the user and then check if the equation/string is a palindrome number or not.

#include<stdio.h>
#include<conio.h>
#define MAXSTK 10

struct stack
{
    int data[MAXSTK];
    int top;
};

int check( char []);
void push(struct stack *, int);
int pop(struct stack *);

void main()
{
    char str[10] = "";
    int flag=0;
    clrscr();
    printf("Enter a String:- ");
    gets(str);
    flag = check(str);

    if(flag == 1)
        printf("String is Palindrome.");
    else
        printf("String is not Palindrome.");
    getch();
}

int check(char str[])
{
    struct stack s1;
    int i = 0;
    s1.top = -1;

    while(str[i] != '\0')
        push(&s1, str[i++]);
    i=0;

    while(str[i] != '\0')
    {
        if(str[i] != pop(&s1))
            return(0);
        i++;
    }
    return(1);
}

void push(struct stack *p, int val)
{
    if(isfull(*p))
    {
        printf("Stack is Full.");
        return;
    }
    p->top++;
    p->data[p->top]=val;
}

int pop(struct stack *p)
{
    int val;
    if(isempty(*p))
    {
        printf("Stack is Empty.");
        return NULL;
    }
    val = p->data[p->top];
    p->top--;
    return(val);
}

int isfull(struct stack p)
{
    return(p.top == MAXSTK-1);
}

int isempty(struct stack p)
{
    return(p.top==-1);
}

Leave a Reply