A line of words made of lowercase English letters and digits, separated by spaces, is given. There may be one or more spaces between words, and spaces at the start or end of the line. Print the words in reverse order on one line, with exactly one space between words. Keep the letters inside each word in their original order. Do not print spaces at the start or end of the line.
Input
line length 1 to 1,000, at least one word
Output
the words in reverse order, separated by single spaces
Example 1 · stdin
the quick brown fox
stdout
fox brown quick the
Example 2 · stdin
keep it simple
stdout
simple it keep
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
Split, reverse, join: the three steps are the same and only the names change. Split: Python split() (no argument), JS/TS trim().split(/\s+/), Java trim().split("\\s+"), C# Split(' ', StringSplitOptions.RemoveEmptyEntries), Craykon Split(). In C and C++, scanf("%s") and cin >> skip spaces for you. Reverse: Python reversed, JS reverse() (changes the array itself), Java Collections.reverse, C# Array.Reverse, C++ std::reverse, Craykon Reverse(). Join: Python " ".join, Java String.join, C# string.Join, Craykon Join(" "). Trap: splitting on a single space, as in split(" "), creates empty pieces where spaces repeat, so the output gets doubled spaces. Reversing the whole string also reverses the letters inside each word.
Pythonmain.py · 2 lines
words = input().split()print(" ".join(reversed(words)))
JavaScriptmain.js · 3 lines
const line = require("fs").readFileSync(0, "utf8").split("\n")[0];const words = line.trim().split(/\s+/);console.log(words.reverse().join(" "));