Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views2 pages

MultipleParenthesisChecker Code

The document contains a C program that implements a stack to check for balanced parentheses in an expression. It defines functions to push and pop elements from the stack, check if the stack is full or empty, and match corresponding parentheses. The main function reads an expression from the user and determines if it is valid based on the matching of parentheses.

Uploaded by

tejaspk11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

MultipleParenthesisChecker Code

The document contains a C program that implements a stack to check for balanced parentheses in an expression. It defines functions to push and pop elements from the stack, check if the stack is full or empty, and match corresponding parentheses. The main function reads an expression from the user and determines if it is valid based on the matching of parentheses.

Uploaded by

tejaspk11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<stdio.

h>
#include<stdlib.h>
#define SIZE 20
int isstackfull(int top)
{
if(top==SIZE-1)
return 1; //full
return 0; //not full
}
int isstackempty(int top)
{
if(top==-1)
return 1; //empty
return 0; //not empty
}
void push(char stack[SIZE],char exp,int *top)
{

if(isstackfull(*top)==1)
{
printf("Stack is full, element cannot be pushed");
return ;
}
stack[++(*top)]=exp;
printf("\nPushed value is %c",stack[*top]);

}
char pop(char stack[30],int *top)
{
if(isstackempty(*top))
return '#';
printf("\nPopped value is %c",stack[*top]);
return stack[(*top)--];
}
int match(char b,char exp)
{
switch(b)
{
case '(':
if(exp==')')
return 1;
break;
case '{':
if(exp=='}')
return 1;
break;
case '[':
if(exp==']')
return 1;
break;
default:
return 0;
}
}
int multi_paren_check(char exp[30])
{

int i=0,top=-1,res;
char b,stack[30];
while(exp[i]!='\0') //while(exp[i])
{
if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')
push(stack,exp[i],&top);
else if(exp[i]==')' || exp[i]=='}' || exp[i]==']')
{
if(isstackempty(top))
return 0;
b=pop(stack,&top);
res=match(b,exp[i]);
if(res==0)
return 0;
}
i++;
}
if(isstackempty(top)==1)
return 1;
return 0;
}

int main()
{
int res;
char exp[30];
printf("Enter the expression:");
scanf("%s",exp);
res=multi_paren_check(exp);
if(res==1)
printf("\nString valid");
else
printf("\nString invalid");
}

You might also like