Answer to Question #255577 in C for vasanth

Question #255577

  Develop a stack using C program to check whether the given expression [a+[b+(c+d)]} is balanced or not




1
Expert's answer
2021-10-24T00:40:12-0400
#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");
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS