Q. Write a function that checks whether a string of characters is palindrome or not. The function should use a stack library (cststack.h) of stack of characters using a static implementation of the stack.
Program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cststack.h"
void main()
{
int i;
char s[MAX], b;
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;
}
Library Function ( .h file )
NOTE: save file name as ' cststack.h'.
#define MAX 50
int top = -1, front = 0;
int stack[MAX];
void push(char);
void pop();
void push(char a)
{
top++;
stack[top] = a;
}
void pop()
{
top--;
}
Output:
Enter the String
dad
dad is palindrome
0 Comments