Perform n steps to convert every digit of a number in the format [ count ] [ digit ] | C # program to convert number to the format [ count ] [ digit ] at every step ; Function to perform every step ; perform N steps ; Traverse in the string ; for last digit ; recur for current string ; Driver Code
using System ; class GFG { public static void countDigits ( string st , int n ) { if ( n > 0 ) { int cnt = 1 , i ; string st2 = " " ; for ( i = 1 ; i < st . Length ; i ++ ) { if ( st [ ( i ) ] == st [ ( i - 1 ) ] ) cnt ++ ; else { st2 += ( ( char ) 0 + ( char ) cnt ) ; st2 += st [ ( i - 1 ) ] ; cnt = 1 ; } } st2 += ( ( char ) 0 + ( char ) cnt ) ; st2 += st [ ( i - 1 ) ] ; countDigits ( st2 , -- n ) ; } else Console . Write ( st ) ; } public static void Main ( ) { string num = "123" ; int n = 3 ; countDigits ( num , n ) ; } }