From 144d88e0497a5d3833f156fc2095fa29d91d2fdd Mon Sep 17 00:00:00 2001 From: CodingBee <48326355+Coding-Bee@users.noreply.github.com> Date: Wed, 2 Oct 2019 18:32:46 +0530 Subject: [PATCH] evaluation of string as palindrome using stack Stack is "lifo"(last in first out) type of data structure in the above program we check if the given string is palindrome using c programming language. --- C/palindormeusing_stack.c | 68 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 C/palindormeusing_stack.c diff --git a/C/palindormeusing_stack.c b/C/palindormeusing_stack.c new file mode 100644 index 00000000..ab12a723 --- /dev/null +++ b/C/palindormeusing_stack.c @@ -0,0 +1,68 @@ + +#include +#include +#include +#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"); + } + } +} + +/* to push a character into stack */ +void push(char a) +{ + top++; + stack[top] = a; +} + +/* to delete an element in stack */ +void pop() +{ + top--; +}