text: Encrypt the string | C # program for above approach ; Function to convert Decimal to Hex ; Function to encrypt the string ; Iterate the characters of the string ; Iterate until S [ i ] is equal to ch ; Update count and i ; Decrement i by 1 ; Convert count to hexadecimal representation ; Append the character ; Append the characters frequency in hexadecimal representation ; Reverse the obtained answer ; Return required answer ; Driver Code ; Given Input ; Function Call
code: using System ; class GFG { static string convertToHex ( int num ) { string temp = " " ; while ( num != 0 ) { int rem = num % 16 ; char c ; if ( rem < 10 ) { c = ( char ) ( rem + 48 ) ; } else { c = ( char ) ( rem + 87 ) ; } temp = temp + c ; num = num / 16 ; } return temp ; } static string encryptString ( string S , int N ) { string ans = " " ; for ( int i = 0 ; i < N ; i ++ ) { char ch = S [ i ] ; int count = 0 ; string hex ; while ( i < N && S [ i ] == ch ) { count ++ ; i ++ ; } i -- ; hex = convertToHex ( count ) ; ans = ans + ch ; ans = ans + hex ; } char [ ] Ans = ans . ToCharArray ( ) ; Array . Reverse ( Ans ) ; ans = new string ( Ans ) ; return ans ; } static void Main ( ) { string S = " abc " ; int N = S . Length ; Console . WriteLine ( encryptString ( S , N ) ) ; } }