Description
This program is used to check balanced parentheses in an Expression.
#include #define MAX 100 void input(); char stack[MAX]; int top = -1; void push(char x) { stack[++top]=x; } char pop() { if(top==-1) return 0; else return stack[top--]; } main() { int choice; do { printf("\n\n----MENU----\n"); printf("1.Input expression\n2.EXIT\n"); printf("\nEnter your choice\n"); scanf("%d",&choice); switch(choice) { case 1:input();break; case 2:printf("\n~~~~Thank You~~~~\n");break; default:printf("\nINVALID INPUT !!!!\n"); } }while(choice!=2); } void input() { top=-1; int i=0,flag=1; char string[100],check; printf("Enter a parantheses string : "); scanf("%s",&string); while(string[i]!='\0') { if(string[i]=='('|| string[i]=='[' || string[i]=='{') push(string[i]); if(string[i]==')') { if(check=pop()!='(') { flag=0;break; } } if(string[i]==']') { if(check=pop()!='[') { flag=0;break; } } if(string[i]=='}') { if(check=pop()!='{') { flag=0;break; } } i++; } if(top!=-1 || flag == 0) printf("\nUNBALANCED parentheses expression !!!"); else printf("\nBALANCED parentheses expression...."); }
One thought on “Parentheses Checker in C”