Expand the string according to the given conditions | C ++ implementation of the approach ; Function to expand and print the given string ; Subtract '0' to convert char to int ; Characters within brackets ; Expanding ; Reset the variables ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void expandString ( string strin ) { string temp = " " ; int j ; for ( int i = 0 ; i < strin . length ( ) ; i ++ ) { if ( strin [ i ] >= 0 ) { int num = strin [ i ] - '0' ; if ( strin [ i + 1 ] == ' ( ' ) { for ( j = i + 1 ; strin [ j ] != ' ) ' ; j ++ ) { if ( ( strin [ j ] >= ' a ' && strin [ j ] <= ' z ' ) || ( strin [ j ] >= ' A ' && strin [ j ] <= ' Z ' ) ) { temp += strin [ j ] ; } } for ( int k = 1 ; k <= num ; k ++ ) { cout << ( temp ) ; } num = 0 ; temp = " " ; if ( j < strin . length ( ) ) { i = j ; } } } } } int main ( ) { string strin = "3 ( ab ) 4 ( cd ) " ; expandString ( strin ) ; }