Decode a median string to the original string | C ++ program to decode a median string to the original string ; function to calculate the median back string ; length of string ; initialize a blank string ; Flag to check if length is even or odd ; traverse from first to last ; if len is even then add first character to beginning of new string and second character to end ; if current length is odd and is greater than 1 ; add first character to end and second character to beginning ; if length is 1 , add character to end ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; string decodeMedianString ( string s ) { int l = s . length ( ) ; string s1 = " " ; bool isEven = ( l % 2 == 0 ) ? true : false ; for ( int i = 0 ; i < l ; i += 2 ) { if ( isEven ) { s1 = s [ i ] + s1 ; s1 += s [ i + 1 ] ; } else { if ( l - i > 1 ) { s1 += s [ i ] ; s1 = s [ i + 1 ] + s1 ; } else { s1 += s [ i ] ; } } } return s1 ; } int main ( ) { string s = " eekgs " ; cout << decodeMedianString ( s ) ; return 0 ; }