text: Check if given Parentheses expression is balanced or not | C program of the above approach ; Function to check if parentheses are balanced ; Initialising Variables ; Traversing the Expression ; It is a closing parenthesis ; This means there are more Closing parenthesis than opening ones ; If count is not zero , It means there are more opening parenthesis ; Driver code
code: #include <stdbool.h> NEW_LINE #include <stdio.h> NEW_LINE bool isBalanced ( char exp [ ] ) { bool flag = true ; int count = 0 ; for ( int i = 0 ; exp [ i ] != ' \0' ; i ++ ) { if ( exp [ i ] == ' ( ' ) { count ++ ; } else { count -- ; } if ( count < 0 ) { flag = false ; break ; } } if ( count != 0 ) { flag = false ; } return flag ; } int main ( ) { char exp1 [ ] = " ( ( ( ) ) ) ( ) ( ) " ; if ( isBalanced ( exp1 ) ) printf ( " Balanced ▁ STRNEWLINE " ) ; else printf ( " Not ▁ Balanced ▁ STRNEWLINE " ) ; char exp2 [ ] = " ( ) ) ( ( ( ) ) " ; if ( isBalanced ( exp2 ) ) printf ( " Balanced ▁ STRNEWLINE " ) ; else printf ( " Not ▁ Balanced ▁ STRNEWLINE " ) ; return 0 ; }