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
def reverse ( s ) : NEW_LINE INDENT stc = [ ] NEW_LINE temp = " " NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if s [ i ] == ' ▁ ' : NEW_LINE stc . append ( temp ) NEW_LINE temp = " " NEW_LINE else : NEW_LINE temp = temp + s [ i ] NEW_LINE DEDENT stc . append ( temp ) NEW_LINE while len ( stc ) != 0 : NEW_LINE INDENT temp = stc [ len ( stc ) - 1 ] NEW_LINE print ( temp , end = " ▁ " ) NEW_LINE stc . pop ( ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT s = " I ▁ Love ▁ To ▁ Code " NEW_LINE reverse ( s ) NEW_LINE