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 stream ; Push each word of the into the stack ; Print the in reverse order of the words ; Driver Code
def printRev ( strr ) : NEW_LINE INDENT strr = strr . split ( " ▁ " ) NEW_LINE st = [ ] NEW_LINE for i in strr : NEW_LINE INDENT st . append ( i ) NEW_LINE DEDENT while len ( st ) > 0 : NEW_LINE INDENT print ( st [ - 1 ] , end = " ▁ " ) NEW_LINE del st [ - 1 ] NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT strr = " geeks ▁ quiz ▁ practice ▁ code " NEW_LINE printRev ( strr ) NEW_LINE DEDENT