{ }codelper
splitting stringsjoining stringsreversing listsP0028

Reverse the word order

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
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(" "));
TypeScriptmain.ts · 4 lines
import { readFileSync } from "node:fs";const line: string = readFileSync(0, "utf8").split("\n")[0];const words: string[] = line.trim().split(/\s+/);console.log(words.reverse().join(" "));
JavaMain.java · 14 lines
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.Arrays;import java.util.Collections;import java.util.List; public class Main {    public static void main(String[] args) throws Exception {        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        List<String> words = Arrays.asList(br.readLine().trim().split("\\s+"));        Collections.reverse(words);        System.out.println(String.join(" ", words));    }}
C#Main.cs · 11 lines
using System; class Program{    static void Main()    {        string[] words = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        Array.Reverse(words);        Console.WriteLine(string.Join(" ", words));    }}
C++main.cpp · 16 lines
#include <algorithm>#include <iostream>#include <string>#include <vector> int main() {    std::vector<std::string> words;    std::string w;    while (std::cin >> w) words.push_back(w);    std::reverse(words.begin(), words.end());    for (size_t i = 0; i < words.size(); i++) {        if (i > 0) std::cout << ' ';        std::cout << words[i];    }    std::cout << "\n";}
Cmain.c · 13 lines
#include <stdio.h> static char words[500][1001]; int main(void) {    int n = 0;    while (n < 500 && scanf("%1000s", words[n]) == 1) n++;    for (int i = n - 1; i >= 0; i--) {        printf("%s", words[i]);        putchar(i > 0 ? ' ' : '\n');    }    return 0;}
Craykonmain.cray · 3 lines
words = Console.ReadLine().Split();words.Reverse();Console.WriteLine(words.Join(" "));