C Program to Identify whether the String is Palindrome or not using Stack

«
»

This is a C Program to identify whether the string is palindrome or not using stack.

Problem Description

This program takes a string and checks whether the string is palindrome or not using stack.

Problem Solution

1. Take a string as input.
2. Store the string in the stack array.
3. Check whether the string is palindrome or not.

advertisement
Program/Source Code

Here is source code of the C Program to identify whether the string is palindrome or not using stack. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Identify whether the String is Palindrome or not using Stack
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #define MAX 50
  8.  
  9. int top = -1, front = 0;
  10. int stack[MAX];
  11. void push(char);
  12. void pop();
  13.  
  14. void main()
  15. {
  16.     int i, choice;
  17.     char s[MAX], b;
  18.     while (1)
  19.     {
  20.         printf("1-enter string\n2-exit\n");
  21.         printf("enter your choice\n");
  22.         scanf("%d", &choice);
  23.         switch (choice)
  24.         {
  25.         case 1:
  26.             printf("Enter the String\n");
  27.             scanf("%s", s);
  28.             for (i = 0;s[i] != '\0';i++)
  29.             {
  30.                 b = s[i];
  31.                 push(b);
  32.             }
  33.             for (i = 0;i < (strlen(s) / 2);i++)
  34.             {
  35.                 if (stack[top] == stack[front])
  36.                 {
  37.                     pop();
  38.                     front++;
  39.                 }
  40.                 else
  41.                 {
  42.                     printf("%s is not a palindrome\n", s);
  43.                     break; 
  44.                 }
  45.             }
  46.             if ((strlen(s) / 2)  =  =  front)
  47.                 printf("%s is palindrome\n",  s);
  48.             front  =  0;
  49.             top  =  -1;
  50.             break;
  51.         case 2:
  52.             exit(0);
  53.         default:
  54.             printf("enter correct choice\n");
  55.         }
  56.     }
  57. }
  58.  
  59. /* to push a character into stack */
  60. void push(char a)
  61. {
  62.     top++;
  63.     stack[top]  =  a;
  64. }
  65.  
  66. /* to delete an element in stack */
  67. void pop()
  68. {
  69.     top--;
  70. }
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[].
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-5 upto the middle element of the array stack[].
7. If every characters of the array is equal, then it is a paliandrome.

advertisement
Runtime Test Cases
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
ugesh
ugesh is not a palindrome
1-enter string
2-exit
enter your choice
1
Enter the String
abccba
abccba is palindrome
1-enter string
2-exit
enter your choice
1
Enter the String
abdbca
abdbca is not a palindrome
1-enter string
2-exit
enter your choice
2

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

advertisement
If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn