Decode the string encoded with the given algorithm | Java implementation of the approach ; Function to decode and print the original String ; To store the decoded String ; Getting the mid element ; Storing the first element of the String at the median position ; If the length is even then store the second element also ; k represents the number of characters that are already stored in the c [ ] ; If String length is odd ; If it is even ; Print the decoded String ; Driver code
class GFG { static void decodeStr ( String str , int len ) { char [ ] c = new char [ len ] ; int med , pos = 1 , k ; if ( len % 2 == 1 ) med = len / 2 ; else med = len / 2 - 1 ; c [ med ] = str . charAt ( 0 ) ; if ( len % 2 == 0 ) c [ med + 1 ] = str . charAt ( 1 ) ; if ( len % 2 == 1 ) k = 1 ; else k = 2 ; for ( int i = k ; i < len ; i += 2 ) { c [ med - pos ] = str . charAt ( i ) ; if ( len % 2 == 1 ) c [ med + pos ] = str . charAt ( i + 1 ) ; else c [ med + pos + 1 ] = str . charAt ( i + 1 ) ; pos ++ ; } for ( int i = 0 ; i < len ; i ++ ) System . out . print ( c [ i ] ) ; } public static void main ( String [ ] args ) { String str = " ofrsgkeeeekgs " ; int len = str . length ( ) ; decodeStr ( str , len ) ; } }