Code For Check Balance Parentheses using C-Programming.
#include<stdio.h>
#include<conio.h>
void push(int *top,char s[],char exp,int max);
int pop(int *top,char s[]);
int isEmpty(int *top);
int isEmpty(int *top)
{
if(*top<=-1)
{return 1;}
else
{ return 0;}
}
void push(int *top,char s[],char exp,int max)
{
if(*top>=max-1)
{printf("Given expression is Not Balanced Parenthese");
}
else
{*top=*top+1;
s[*top]=exp;
}
}
int pop(int *top,char s[])
{
if(*top<=-1)
{ printf("Given expression is Not Balanced Parenthese");
return 1;}
else
{
int item=s[*top];
*top=*top-1;
return item;
}
}
void main()
{
int *top,max=10,valid=0,len,i;
char exp[10],s[10];
*top=-1;
clrscr();
printf("Enter Expression : ");
gets(exp);
len=strlen(exp);
for(i=0;i<len;i++)
{
if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')
{
push(top,s,exp[i],max);
}
else if(exp[i]==')' || exp[i]=='}' || exp[i]==']')
{
pop(top,s);
}
else
{
printf("Error:");
}
}
if(isEmpty(top))
{
printf("Given expression is Balanced Parenthese");
}
else
{
printf("Given expression is Not Balanced Parenthese");
}
getch();
}
#include<conio.h>
void push(int *top,char s[],char exp,int max);
int pop(int *top,char s[]);
int isEmpty(int *top);
int isEmpty(int *top)
{
if(*top<=-1)
{return 1;}
else
{ return 0;}
}
void push(int *top,char s[],char exp,int max)
{
if(*top>=max-1)
{printf("Given expression is Not Balanced Parenthese");
}
else
{*top=*top+1;
s[*top]=exp;
}
}
int pop(int *top,char s[])
{
if(*top<=-1)
{ printf("Given expression is Not Balanced Parenthese");
return 1;}
else
{
int item=s[*top];
*top=*top-1;
return item;
}
}
void main()
{
int *top,max=10,valid=0,len,i;
char exp[10],s[10];
*top=-1;
clrscr();
printf("Enter Expression : ");
gets(exp);
len=strlen(exp);
for(i=0;i<len;i++)
{
if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')
{
push(top,s,exp[i],max);
}
else if(exp[i]==')' || exp[i]=='}' || exp[i]==']')
{
pop(top,s);
}
else
{
printf("Error:");
}
}
if(isEmpty(top))
{
printf("Given expression is Balanced Parenthese");
}
else
{
printf("Given expression is Not Balanced Parenthese");
}
getch();
}
Comments
Post a Comment