#include<stdio.
h>
#include<stdlib.h>
#define SIZE 20
int isstackfull(int top)
{
if(top==SIZE-1)
return 1; //full
return 0; //not full
}
int isstackempty(int top)
{
if(top==-1)
return 1; //empty
return 0; //not empty
}
void push(char stack[SIZE],char exp,int *top)
{
if(isstackfull(*top)==1)
{
printf("Stack is full, element cannot be pushed");
return ;
}
stack[++(*top)]=exp;
printf("\nPushed value is %c",stack[*top]);
}
char pop(char stack[30],int *top)
{
if(isstackempty(*top))
return '#';
printf("\nPopped value is %c",stack[*top]);
return stack[(*top)--];
}
int match(char b,char exp)
{
switch(b)
{
case '(':
if(exp==')')
return 1;
break;
case '{':
if(exp=='}')
return 1;
break;
case '[':
if(exp==']')
return 1;
break;
default:
return 0;
}
}
int multi_paren_check(char exp[30])
{
int i=0,top=-1,res;
char b,stack[30];
while(exp[i]!='\0') //while(exp[i])
{
if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')
push(stack,exp[i],&top);
else if(exp[i]==')' || exp[i]=='}' || exp[i]==']')
{
if(isstackempty(top))
return 0;
b=pop(stack,&top);
res=match(b,exp[i]);
if(res==0)
return 0;
}
i++;
}
if(isstackempty(top)==1)
return 1;
return 0;
}
int main()
{
int res;
char exp[30];
printf("Enter the expression:");
scanf("%s",exp);
res=multi_paren_check(exp);
if(res==1)
printf("\nString valid");
else
printf("\nString invalid");
}