Q:

C program to check String is Palindrome or not using Stack

0

This is a C Program to identify whether a string is palindrome or not using stack. A palindrome string is a string that reads the same backward as forward. Some palindrome string examples are "C", "CC", "CCC", "madam", "abba", etc.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C program to check String is Palindrome or not using Stack - Source code

int top = -1, front = 0;
int stack[MAX];
 
void push(char);
void pop();
 
void main()
{
    int i, c;
    char s[MAX], b;
    while (1)
    {
        printf("1-enter string\n2-exit\n");
        printf("enter your choice\n");
        scanf("%d", &c);
        switch (c)
        {
        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("Please enter correct choice\n");
        }
    }
}
 
/* to push a character into stack */
void push(char a)
{
    top++;
    stack[top]  =  a;
}
 
/* to delete an element in stack */
void pop()
{
    top--;
}

Program Output

1-enter string
2-exit
enter your choice
1
Enter the String
madam
madam is palindrome
1-enter string
2-exit
enter your choice
1
Enter the String
abbbcbbba
abbbcbbba is palindrome
1-enter string
2-exit
enter your choice
compscibits
Enter the String
compscibits is not a palindrome
1-enter string
2-exit
enter your choice

Program Explanation

1. Take a string as input and store it in the array s[].

2. Load each character of the array s[] into the array stack[] using a for loop.

3. Use variables front and top to represent the last and top element of the array stack[].

4. 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.

5. If they are not equal, then print the output as “It is not a palindrome”.

6. Compare the elements in the steps 4 and 5 upto the middle element of the array stack[].

7. If every characters of the array is equal, then it is a palindrome.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
<< C program to find the length of a string without u...