You are on page 1of 2

#include<stdio.

h>
#include<stdlib.h>
int state=0;
char str[100];
int top=0;
void push(char c)
{
printf("PUSHING %c\n",c);
printf("top of the stack is %c",c);
str[top++]=c;
}
void pop()
{
if(top>-1)
{
printf("POPPING %d",top);
top--;
}
else
{
printf("cant pop anymore");
}
}
void main()
{
int i=0;
char str1[100];
char z='z';
printf("IMPLEMENTING a^nb^n");
printf("ENTER THE STRING");
scanf("%s",str1);
push(z);
for(i=0;str1[i]!='\0';i++)
{
switch(state)
{
case 0: if(str1[i]=='a')
{
push(str1[i]);
state=0;
break;
}
case 1: if(str1[i]=='b')
{
pop();
state=1;
break;
}
}
if(str1[i]=='b' && str1[i+1]=='a')
{
printf("ERROR");
}
}
printf("THE CURRENT STATE IS %d",state);
for(i=0;str1[i]!='\0';i++)
{
printf("%c",str[i]);
}
if(top!=1)
{
printf("ERROR");
}
}

You might also like