Remove spaces from a given string | Function to remove all spaces from a given string ; To keep track of non - space character count ; Traverse the given string . If current character is not space , then place it at index ' count + + ' ; here count is incremented ; Driver code
function removeSpaces ( str ) { var count = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) if ( str [ i ] !== " " ) str [ count ++ ] = str [ i ] ; return count ; } var str = " " . split ( " " ) ; var i = removeSpaces ( str ) ; document . write ( str . join ( " " ) . substring ( 0 , i ) ) ;