can anyone tell me about how to write a code which will tell a string whether its a palindrome or no using stacks ( which ignores case , space and punctuation ??
i did this part
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct node
{
char ch;
struct node* link;
}NODE;
typedef struct stack
{
int count;
NODE *top;
}STACK;
STACK* CreateStack();
char PushStack(STACK* sk, char ch);
char PopStack(STACK* sk);
int EmptyStack(STACK* sk);
int CheckPalindrome(STACK* s,STACK* r);
void PopTop(STACK* sk, char *chOut);
STACK* CreateStack()
{
STACK* sk = (STACK*) malloc(sizeof(STACK));
sk->count = 0;
sk->top = NULL;
return sk;
}
char PushStack(STACK* sk, char ch)
{
NODE* pnew = (NODE*) malloc (sizeof(NODE));
pnew->ch = ch;
pnew->link = sk->top;
sk->top = pnew;
sk->count++;
return pnew->ch;
}
char PopStack(STACK* sk)
{
NODE* temp;
char pop;
pop=sk->top->ch;
temp = sk->top;
sk->top = sk->top->link;
sk->count--;
free(temp);
return pop;
}
int EmptyStack(STACK* sk)
{
return sk->count==0;
}
int CheckPalindrome(STACK* s,STACK* r)
{
char st,re,temp;
int x=0;
while (!EmptyStack(s))
{
st=PopStack(s);
re=PopStack(r);
if (st!=re)
x++;
}
return x;
}
void PopTop(STACK* sk, char *chOut)
{
*chOut = sk->top->ch;
}
main()
{
char str[50],rev[50],temp;
int i=0,x;
char *ptr, *revptr;
printf ("Enter the string\n");
scanf("%[^\n]s",str);
STACK* stk=CreateStack();
STACK* tempstk=CreateStack();
ptr = str;
printf("\nHere is the stack using linked list: ");
while(*ptr)
{
temp=PushStack(tempstk, *ptr);
temp=PushStack(stk, *ptr);
printf("%c ",temp);
ptr++;
}
ptr = str;
while (!EmptyStack(tempstk))
{
temp=PopStack(tempstk);
rev=temp;
i++;
}
rev='\0';
STACK* revstk=CreateStack();
printf("\nHere is the Reverse Stack using Linked list: ");
revptr = rev;
while(*revptr)
{
temp=PushStack(revstk, *revptr);
printf("%c ",temp);
revptr++;
}
x=CheckPalindrome(stk,revstk);
if(x==0)
printf("\n The string is Palindrome.");
else
printf("\n The string is not Palindrome.");
}