Check whether the parenthesis are balanced in the given expression.

Program

#include<stdio.h>
char arr[100];
int top=-1;
void push(char a)
{
    arr[top]=a;
    top++;
}
void pop()
{
    if(top==-1)
    {
        printf("Invalid expression");
        exit(0);
    }
    else
    {
        top--;
    }
}
void find_top()
{
    if(top==-1)
    {
        printf("Valid");
    }
    else
    {
        printf("Invalid");
    }
}
int main()
{
    char a[100];
    int i;
    scanf("%s",a);
    for(i=0;a[i]!='\0';i++)
    {
        if(a[i]=='(')
        push(a[i]);
        else if(a[i]==')')
        pop();
    }
    find_top();
    return 0;
}



Output:

(c+d))                                                                                                                                   
Invalid                                                                                                                                          
         
((a+b)(c+d))                                                                                                                                   
Valid                                                                                                                                          
         

(a+b)b                                                                                                                                         
Valid      

Comments

Popular posts from this blog

c++ program to print string along the diagonals of the matrix.

C program to print the following Pattern

Kollywood Game