Reverse words in string Python

PROGRAM TO REVERSE THE STRING BY CONSIDERING EACH WORD OF THE STRING



# Function to reverse the words
# of a given strring
def printRev(strr):
      
    # Stack to store each
    # word of the strring
    strr = strr.split(" ")
    st = []
  
    # Store the whole strring
    # in strream
    for i in strr:
  
        # Push each word of the
        # into the stack
        st.append(i)
  
    # Print the in reverse
    # order of the words
    while len(st) > 0:
        print(st[-1], end = " ")
        del st[-1]
  
# Driver Code
if __name__ == '__main__':
      
    strr = "geeks quiz practice code"
      
    printRev(strr)


OUTPUT
code practice quiz

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java