Reverse words in a given string | Set 2 | Function to reverse the words of a given String ; Stack to store each word of the String ; Store the whole String in String stream ; Push each word of the String into the stack ; Print the String in reverse order of the words ; Driver Code
function printRev ( str ) { let st = [ ] ; let ss = str . split ( " " ) ; for ( let temp = 0 ; temp < ss . length ; temp ++ ) { st . push ( ss [ temp ] ) ; } while ( st . length != 0 ) { document . write ( st . pop ( ) + " " ) ; } } let str ; str = " " ; printRev ( str ) ;