Program to replace every space in a string with hyphen | Get the String ; Traverse the string character by character . ; Changing the ith character to ' - ' if it 's a space. ; Print the modified string .
var str = " " ; var newStr = str . split ( " " ) ; for ( var i = 0 ; i < newStr . length ; ++ i ) { if ( newStr [ i ] === " " ) { newStr [ i ] = " " ; } } document . write ( newStr . join ( " " ) + " " ) ;