Reverse String according to the number of words | Reverse the letters of the word ; Temporary variable to store character ; Swapping the first and last character ; This function forms the required string ; Checking the number of words present in string to reverse ; Reverse the letter of the words ; Driver Code
function reverse ( str , start , end ) { let temp ; while ( start <= end ) { temp = str [ start ] ; str [ start ] = str [ end ] ; str [ end ] = temp ; start ++ ; end -- ; } } function reverseletter ( str , start , end ) { let wstart , wend ; for ( wstart = wend = start ; wend < end ; wend ++ ) { if ( str [ wend ] == ' ' ) { continue ; } while ( wend <= end && str [ wend ] != ' ' ) { wend ++ ; } wend -- ; reverse ( str , wstart , wend ) ; } } let str = " " . split ( " " ) ; reverseletter ( str , 0 , str . length - 1 ) ; document . write ( ( str ) . join ( " " ) ) ;