Reverse a text in two different ways: reverse the entire text or reverse word by word.
# coding=utf-8
"""
Reverse a given text
# ----- REVERSE THE ENTIRE SENTENCE -----
# This method reverses the entire sentence
# The reverse_text function directly performs this operation
# ----- REVERSE WORD BY WORD -----
# This method reverses the sentence word by word
# First, split the sentence by space character and assign to an array
# Then reverse each element of this array
# The reverse_text function already reverses the incoming string
# So we will send each string one by one to that function
# After reversing, we will write our array together with " ".join
"""
input_text = input("Enter a sentence: ")
reversed_text = ""
def reverse_text(text, reversed):
for i in range(len(text) - 1, -1, -1):
reversed += text[i]
return reversed
words = []
for i in input_text.split(' '):
words.append(reverse_text(i, ""))
completely_reversed = reverse_text(input_text, reversed_text)
word_by_word_reversed = " ".join(words)
print(
"""
Original text: {}
Completely reversed: {}
Word by word reversed: {}
""".format(input_text, completely_reversed, word_by_word_reversed))