Saturday 12 October 2013

c program to check if given expression is correctly parenthesized

#include<stdio.h>
#include<conio.h>
int top=-1,st[20];
void push(char c)
{
st[++top]=c;
}
void pop()
{
top=top-1;
}
void caltop()
{
if(top==-1)
{
printf("\n\n expression is valid");
}
else
{
printf("expression is invalid");
}
}

void main()
{
int i,a[20];
clrscr();
printf("\n enter the expression");
scanf("%s",a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]==')')
push(a[i]);
else if(a[i]==')')
pop();
}
caltop();
getch();
}

No comments:

Post a Comment