Move spaces to front of string in single traversal | Function to find spaces and move to beginning ; Traverse from end and swap spaces ; Driver code
function moveSpaceInFront ( str ) { let i = str . length - 1 ; for ( let j = i ; j >= 0 ; j -- ) if ( str [ j ] != ' ' ) { let c = str [ i ] ; str [ i ] = str [ j ] ; str [ j ] = c ; i -- ; } } let str = " " . split ( " " ) ; moveSpaceInFront ( str ) ; document . write ( ( str ) . join ( " " ) ) ;