How to reverse the order of words in a sentence
If you are given the sentence “you shall not pass” and tasked with reversing the sentence’s word order; then the sentence becomes “pass not shall you”.
Consider the following illustration, which highlights one possible solution to this problem:
This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
Implementation
One interesting thing about this problem is that all of its solutions are very language-dependent. Consider the implementations shown below:
C++
The following is an implementation of the method described above:
This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
Python
Python has built-in functions to split the string, reverse the order of its words, and then join the words again. This implementation is shown below:
This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
Java
The solution in Java uses a built-in split function that is followed by the string being saved in reverse:
This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved