Reverse a string preserving space positions | Function to reverse the string and preserve the space position ; Mark spaces in result ; Traverse input string from beginning and put characters in result from end ; Ignore spaces in input string ; Ignore spaces in result . ; Driver code
def reverses ( st ) : NEW_LINE INDENT n = len ( st ) NEW_LINE result = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( st [ i ] == ' ▁ ' ) : NEW_LINE INDENT result [ i ] = ' ▁ ' NEW_LINE DEDENT DEDENT j = n - 1 NEW_LINE for i in range ( len ( st ) ) : NEW_LINE INDENT if ( st [ i ] != ' ▁ ' ) : NEW_LINE INDENT if ( result [ j ] == ' ▁ ' ) : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT result [ j ] = st [ i ] NEW_LINE j -= 1 NEW_LINE DEDENT DEDENT return ' ' . join ( result ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " internship ▁ at ▁ geeks ▁ for ▁ geeks " NEW_LINE print ( reverses ( st ) ) NEW_LINE DEDENT