Solved programming problem

Determine Some Bracket are Balanced or Not with Stack(array) in

Source code

#include <stdio.h>

#include<string.h>

#define MAX 10

struct STACK

{

    int top;

    char c[MAX];

};

typedef struct STACK stack;

stack stc,stcrev; ///create two STACK

void balanced(char input[])

{

    char last_char;

    int i,len;

    len = strlen(input);

    for(i=0; i<len; i++)

    {

        if(input[i]== '(' ||input[i]== '{' ||input[i]== '[')  ///insert only (,{,[  in the 1st stack

        {

            stc.c[stc.top]=input[i];

            stc.top++;

        }

    }

    stcrev.top=0;

    for(i=len-1; i>=len/2; i--)  ///else every think insert in the 2nd stack

    {

        stcrev.c[stcrev.top]=input[i];

        stcrev.top++;

    }

    stcrev.top--;

    stc.top--;

    while(stc.top!=0 && stcrev.top!=0) ///compare two stack

    {

        if(stc.c[stc.top]=='(')

        {

            if(stcrev.c[stcrev.top]==')')

            {

                stcrev.top= stcrev.top-1;

            }

        }

        else if(stc.c[stc.top]=='{')

        {

            if(stcrev.c[stcrev.top]=='}')

            {

                stcrev.top= stcrev.top-1;

            }

        }

        else if(stc.c[stc.top]=='[')

        {

            if(stcrev.c[stcrev.top]==']')

            {

                stcrev.top= stcrev.top-1;

            }

        }

        stc.top=stc.top-1;

    }

    if(stc.top==0 && stcrev.top==0)

    {

        printf("
 Balanced
");

    }

    else printf("
 Not Balanced
");

}

int main()

{

    char input[100];

    printf("Please Enter Some Bracket: ");

    gets(input);

    stc.top=0;

    stcrev.top=0;

    balanced(input);

    return 0;

}

Output

Please Enter Some Bracket: [{()}]

 Balanced

 

Join the discussion

Add your comment