Parentheses Code
C code
#include <stdio.h>
#include <stdlib.h>
#define max 50
int arepair(char opening,char closing)
{
if(opening == '(' && closing == ')') return 1;
else if(opening == '{' && closing == '}') return 1;
else if(opening == '[' && closing == ']') return 1;
return 0;
}
int main(int argc, char const *argv[])
{
char stk[max],exp[100];
int top,i;
top=-1;
printf("Enetr sting");
gets(exp);
printf("%s\n",exp);
for (int i = 0; exp[i]!='\0'; i++)
{
if(exp[i]=='('||exp[i]=='['||exp[i]=='{')
{
top++;
stk[top]=exp[i];
}
else if(exp[i]==')'||exp[i]=='}'||exp[i]==']')
{
if(0==arepair(stk[top],exp[i]))
printf("NOT paired");
else
top--;
}
}//for
if(top==-1)
printf("Exp is balance");
else
printf("NOT\n");
return 0;
}
OUTPUT
C:\Users\rushi\Desktop\1>gcc parant.c
C:\Users\rushi\Desktop\1>a.exe
Enetr sting{()[()()]}
{()[()()]}
Exp is balance
C:\Users\rushi\Desktop\1>a.exe
Enetr sting{][}
{][}
NOT pairedNOT pairedNOT
C:\Users\rushi\Desktop\1>