0% found this document useful (0 votes)
5 views3 pages

Prac09 Paranthesis Checker

This C program implements a stack to check if a given expression containing parentheses is balanced. It defines functions to push and pop characters from the stack, check for matching pairs of parentheses, and determine if the entire expression is balanced. The main function prompts the user for input and outputs whether the expression is balanced or not.

Uploaded by

manasi wani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Prac09 Paranthesis Checker

This C program implements a stack to check if a given expression containing parentheses is balanced. It defines functions to push and pop characters from the stack, check for matching pairs of parentheses, and determine if the entire expression is balanced. The main function prompts the user for input and outputs whether the expression is balanced or not.

Uploaded by

manasi wani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

INPUT :

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_SIZE 100

// Global variables for stack and top

char stack[MAX_SIZE];

int top = -1;

// Function to push a character onto the stack

void push(char data) {

if (top == MAX_SIZE - 1) {

printf("Overflow stack!\n");

return;

top++;

stack[top] = data;

// Function to pop a character from the stack

char pop() {

if (top == -1) {

printf("Empty stack!\n");

return ' ';

char data = stack[top];

top--;

return data;

// Function to check if two characters form a matching pair of parentheses

int is_matching_pair(char char1, char char2) {


if (char1 == '(' && char2 == ')') {

return 1;

} else if (char1 == '[' && char2 == ']') {

return 1;

} else if (char1 == '{' && char2 == '}') {

return 1;

} else {

return 0;

// Function to check if the expression is balanced

int isBalanced(char* text) {

int i;

for (i = 0; i < strlen(text); i++) {

if (text[i] == '(' || text[i] == '[' || text[i] == '{') {

push(text[i]);

} else if (text[i] == ')' || text[i] == ']' || text[i] == '}') {

if (top == -1) {

return 0; // If no opening bracket is present

} else if (!is_matching_pair(pop(), text[i])) {

return 0; // If closing bracket doesn't match the last opening bracket

if (top == -1) {

return 1; // If the stack is empty, the expression is balanced

} else {

return 0; // If the stack is not empty, the expression is not balanced

// Main function

int main() {
char text[MAX_SIZE];

printf("Input an expression in parentheses: ");

scanf("%s", text);

// Check if the expression is balanced or not

if (isBalanced(text)) {

printf("The expression is balanced.\n");

} else {

printf("The expression is not balanced.\n");

return 0;

OUTPUT:

You might also like