Reverse substrings between each pair of parenthesis | C # implementation of the approach ; Function to return the modified string ; Push the index of the current opening bracket ; Reverse the substring starting after the last encountered opening bracket till the current character ; To store the modified string ; Driver code
using System ; using System . Collections . Generic ; using System . Text ; class GFG { static void reverse ( char [ ] A , int l , int h ) { if ( l < h ) { char ch = A [ l ] ; A [ l ] = A [ h ] ; A [ h ] = ch ; reverse ( A , l + 1 , h - 1 ) ; } } static string reverseParentheses ( string str , int len ) { Stack < int > st = new Stack < int > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' ( ' ) { st . Push ( i ) ; } else if ( str [ i ] == ' ) ' ) { char [ ] A = str . ToCharArray ( ) ; reverse ( A , st . Peek ( ) + 1 , i ) ; str = new string ( A ) ; st . Pop ( ) ; } } string res = " " ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] != ' ) ' && str [ i ] != ' ( ' ) { res += str [ i ] ; } } return res ; } static public void Main ( ) { string str = " ( skeeg ( for ) skeeg ) " ; int len = str . Length ; Console . WriteLine ( reverseParentheses ( str , len ) ) ; } }