Develop a stack using C program to check whether the given expression [a+[b+(c+d)]} is balanced or not
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int up = -1;
char stack[100];
void push(char);
void pop();
void get_up();
void main()
{
int m;
char x[100];
printf("Input the expression\n");
scanf("%s", &x);
for (m = 0; x[m] != '\0';m++)
{
if (x[m] == '(')
{
push(x[m]);
}
else if (x[m] == ')')
{
pop();
}
}
get_up();
}
void push(char x)
{
stack[up] = x;
up++;
}
void pop()
{
if (up == -1)
{
printf("expression is not balanced\n");
exit(0);
}
else
{
up--;
}
}
void get_up()
{
if (up == -1)
printf("\nExpression is balanced\n");
else
printf("\nExpression is not balanced\n");
}
Comments
Leave a comment