Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Balanced Parenthesis

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int main()

char exp[100],str[100];

printf("\nEnter the expression");

scanf("%s",exp);

int top= -1,i,n=strlen(exp);

char temp;

for(i=0;i<n;i++)

if((exp==']'||exp[i]=='}'||exp[i]==')')&&top== -1)

break;

if((exp=='['||exp[i]=='{'||exp[i]=='('))

top++;

str[top]=exp[i];

else

temp=str[top];

top--;

if((temp=='('&&exp[i]==')')||(temp=='{'&&exp[i]=='}')||(temp=='['&&exp[i]==']'))

continue;
Balanced Parenthesis
}

else

break;

if(i==n&&top==-1)

printf("\nBalanced expression!!!!!");

else

printf("\nNot a balanced expression!!!!!");

You might also like