Print Bracket Number | C # implementation to print the bracket number ; function to print the bracket number ; used to print the bracket number for the left bracket ; used to obtain the bracket number for the right bracket ; traverse the given expression ' exp ' ; if current character is a left bracket ; print ' left _ bnum ' , ; Push ' left _ bum ' on to the stack ' right _ bnum ' ; increment ' left _ bnum ' by 1 ; else if current character is a right bracket ; print the top element of stack ' right _ bnum ' it will be the right bracket number ; Pop the top element from the stack ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void printBracketNumber ( string exp , int n ) { int left_bnum = 1 ; Stack < int > right_bnum = new Stack < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( exp [ i ] == ' ( ' ) { Console . Write ( left_bnum + " ▁ " ) ; right_bnum . Push ( left_bnum ) ; left_bnum ++ ; } else if ( exp [ i ] == ' ) ' ) { Console . Write ( right_bnum . Peek ( ) + " ▁ " ) ; right_bnum . Pop ( ) ; } } } static void Main ( ) { string exp = " ( a + ( b * c ) ) + ( d / e ) " ; int n = exp . Length ; printBracketNumber ( exp , n ) ; } }