Tuesday 10 July 2012

Check Palindrome Using Stack


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<string>

struct stack
{
int data[15];
int tos;


void init();
void push(char);
int pop();
void full();
void empty();

};


void stack::init()
{
tos=-1;
}


void stack::push(char val)
{
if(tos<15)
{
tos++;
data [tos]=val;
printf("\n %c \n",val);

}
else
{
full();
}
}


int stack::pop()
{

if(tos!=-1)
{
return data[tos--];
}
else
{
empty();
return -1;
}
}



void stack::full()
{
printf("\n Overflow-Cannot store more value\n");
}



void stack::empty()
{
printf("\n Underflow-Have no value to show\n");
}


void main()
{

stack st1;
int s=0,o=0,len,cout=0;
char str[50],val,i,a[25];

printf("\nEnter the string == ");
gets(str);
len=strlen(str);

st1.init();

int x=0,tos=0,c,h,length,length1;

for(int k=0;k<8;k++)
{
printf("Press 1 to store(push)\n");
printf("Enter 2 to pop\n");
printf("Press 3 to Clear Screen\n\n\tOption:");
i=getche();
if(i=='1')
{
if(tos>=15)
st1.full();
else
{
printf("\nEnter something:");
fflush(stdin);
scanf("%c",&val);
st1.push(val);
tos++;
}
}

else if(i=='2')
{
c=st1.pop();
while(s>=o)
{
a[s]=c;
h=o;
o++;
}
s++;
printf("\n\nPoped value == %c\n\n",c);

}
else if(i=='3')
{
                 system("cls");
}
}


printf("\n\nPoped array is: ");
cout=0;
for(int k=0;k<h+1;k++)
{
cout++;
printf("%c ,",a[k]);
}

printf("The length of poped array is == %d",cout);


getch();

int p=0,counter=0;
while(a[p]!=-52)
{
if(a[p]!=str[p])
{
counter++;
}
p++;
}

if(counter==0)
{
printf("\nPalindrome");
}
else
{
printf("\nNot Palindrome");
}

getche();
}

No comments:

Post a Comment