text: Encrypt the given string with the following operations | C # implementation of the above approach : ; Function to return the encrypted string ; Reduce x because rotation of length 26 is unnecessary ; calculate the frequency of characters ; If the frequency of current character is even then increment it by x ; Else decrement it by x ; Return the count ; Driver code
code: using System ; class GFG { static int MAX = 26 ; public static char [ ] encryptStr ( String str , int n , int x ) { x = x % MAX ; char [ ] arr = str . ToCharArray ( ) ; int [ ] freq = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq [ arr [ i ] - ' a ' ] % 2 == 0 ) { int pos = ( arr [ i ] - ' a ' + x ) % MAX ; arr [ i ] = ( char ) ( pos + ' a ' ) ; } else { int pos = ( arr [ i ] - ' a ' - x ) ; if ( pos < 0 ) pos += MAX ; arr [ i ] = ( char ) ( pos + ' a ' ) ; } } return arr ; } public static void Main ( String [ ] args ) { String s = " abcda " ; int n = s . Length ; int x = 3 ; Console . WriteLine ( encryptStr ( s , n , x ) ) ; } }