TEAM CODING-BCA

C Program to check Bracket Validation using Stack

C Program to check Bracket Validation using Stack :

#include<stdio.h>
#include<string.h>
void main()
{
char exp[100];
char stk[100];
int top=-1,f=0,i;
printf("\nEnter the Expression: ");
scanf("%s",exp);
for(i=0;i<strlen(exp);i++)
{
if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')
{
stk[++top]=exp[i];
}
else if(exp[i]==')')
{
if(stk[top]!='(')
{
f=1;
break;
}
else
top--;
}
else if(exp[i]=='}')
{
if(stk[top]!='{')
{
f=1;
break;
}
else
top--;
}
else if(exp[i]==']')
{
if(stk[top]!='[')
{
f=1;
break;
}
else
top--;
}
}
if(f==0)
printf("\nValid Bracket Expression");
else
printf("\nInvalid Bracket Expression!!!");
}

OUTPUT

OUTPUT

No comments:

Post a Comment