Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create balanced & unbalanced parenthesis #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aditi-disaster
Copy link

#include<stdio.h>
#include<string.h>
#define SIZE 20
char STACK[SIZE];
int top=-1;
void push(char item)
{
if(top==SIZE-1)
printf("\nSTACK IS OVERFLOW");
else
{
top++;
STACK[top]=item;
}
}
void pop()
{
if(top==-1)
printf("\nunderflow");
else
top--;
}
int main(void)
{
char exp[SIZE];
int len,i;
printf("\nenter the expression");
scanf("%s",exp);
len=strlen(exp);
for(i=0;i<len;i++)
{

	if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')
	{
		push(exp[i]);
		continue;
	}
	else if(exp[i]==')' || exp[i]=='}' || exp[i]==']')
	{
		if(exp[i]==')')
		{
			if(STACK[top]=='(')
			{
				pop();
			}
			else
			{
				printf("\nUNBALANCED PARENTHESIS");
				return 0;
			}
		}
		else if(exp[i]=='}')
		{
			if(STACK[top]=='{')
			{
				pop();
			}
			else
			{
				printf("\nUNBALANCED PARENTHESIS");
				return 0;
			}
		}
			else if(exp[i]==']')
			{
				if(STACK[top]=='[')
				{
					pop();
				}
				else
				{
					printf("\nUNBALANCED PARENTHESIS");
					return 0;
				}
			}
		}
	}

if(top!=-1)
printf("\nUNBALANCED PARENTHESIS");
else
printf("\nBALANCED PARENTHESIS");
return 0;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant