Reverse the Words of a String using Stack | Function to reverse the words of the given String without using strtok ( ) . ; Create an empty String stack ; Create an empty temporary String ; Traversing the entire String ; Push the temporary variable into the stack ; Assigning temporary variable as empty ; For the last word of the String ; Get the words in reverse order ; Driver code
function reverse ( s ) { let stc = [ ] ; let temp = " " ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) { stc . push ( temp ) ; temp = " " ; } else { temp = temp + s [ i ] ; } } stc . push ( temp ) ; while ( stc . length != 0 ) { temp = stc [ stc . length - 1 ] ; document . write ( temp + " " ) ; stc . pop ( ) ; } } let s = " " ; reverse ( s ) ;