Balanced Expression
Write A C Program To Check Whether A Given Expression is balanced In Case Of Parenthesis Source Code: #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> void main() { char stk[100],exp[100]; int i,top; top=-1; clrscr(); printf("enter an expression\n"); gets(exp); for(i=0;exp[i]!='\0';i++) { if(exp[i]=='('||exp[i]=='{'||exp[i]=='[') { top=top+1; stk[top]=exp[i]; } else if(exp[i]==')') { if(stk[top]=='(') { top--; } else { printf("unbalanced expression"); exit(0); } } else if(exp[i]=='}') { if(stk[top]=='{') { top--; } else { printf("unbalanced expression"); exit(0); } } else if(exp[i]==']') { if(stk[top]=='[') { top--; } else { printf("unbalanced expression"); exit(0); } } } if(top==-1) { printf("expression is balanced\n"); } else { printf("expression is not balanced\n"); } getch(); } output: enter an expression 1+2*(2+3)+{4+5} expression is balanced enter an expression (1+2)*{2*3{ expression is not balanced
Tags:
C Program
0 comments