Dec-10-2019, 05:00 PM
The code assesses the sentence from the end to the beginning. The first while loop checks each character starting from the end until it finds a space. Once it finds a space, it iterates from the index start until index end and writes the word to the result. Then, it continues. The last loop basically does the same thing as the first one to pick up the first word in the original sentence.
Now, this can be written more concisely in Python.
Now, this can be written more concisely in Python.
def wordReverse(str): words = str.split() # Split sentence by spaces into a list return " ".join(words[::-1])