PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int
top=-1,d=0;
char
ch,stack[30];
clrscr();
printf("\n\n$$$
BALANCING SYMBOLS $$$\n\n");
printf("Check
whether the given expression is balanced or not\n");
printf("Enter
the expression:\t");
while(ch=getchar(),ch!='#')
{
switch(ch)
{
case
'(':stack[++top]=ch;break;
case
'{':stack[++top]=ch;break;
case
'[':stack[++top]=ch;break;
case
')':if(stack[top]=='(')
{
top=top-1;
}
else
{
d=d+1;
}
break;
case
'}':if(stack[top]=='{')
{
top=top-1;
}
else
{
d=d+1;
}
break;
case
']':if(stack[top]=='[')
{
top=top-1;
}
else
{
d=d+1;
}
break;
}
}
if(top==-1
&& d==0)
{
printf("Balanced
Expression\n");
}
else
{
printf("Unbalanced
Expression\n");
}
getch();
}
OUTPUT:
$$$ BALANCING SYMBOLS $$$
Check whether the given expression is balanced or not
Enter the expression:
{[(a+b)*c]-[(c*d)+b]}#
Balanced Expression
$$$ BALANCING SYMBOLS $$$
Check whether the given expression is balanced or not
Enter the expression:
{(a*b]/(c+d]}#
Unbalanced Expression
No comments:
Post a Comment