Reverse words in a given string | Set 2 | Java Program to implement the above approach ; 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
import java . util . * ; class GFG { static void printRev ( String str ) { Stack < String > st = new Stack < String > ( ) ; String [ ] ss = str . split ( " ▁ " ) ; for ( String temp : ss ) { st . add ( temp ) ; } while ( ! st . isEmpty ( ) ) { System . out . print ( st . peek ( ) + " ▁ " ) ; st . pop ( ) ; } } public static void main ( String [ ] args ) { String str ; str = " geeks ▁ quiz ▁ practice ▁ code " ; printRev ( str ) ; } }