Lesson 1.6 · Write it
Swap lines
A whole programPython or Java5 tests, 3 of them hidden
On a phone? Put the lines in order instead
Write a program that reads two lines typed by the user, one after the other, and prints them in the opposite order: the second line first, then the first line.
- The first line typed is printed second, and the second line typed is printed first.
- Each line is printed exactly as it was typed.
- Print nothing else. Spaces at the end of a line are ignored.
Your program is run several times, with different lines typed each time. Only what it prints is checked.
In Python, read each line with input() and save it in a variable. In Java, the page starts you with a class, its main method and a Scanner called scanner: read each line with scanner.nextLine() and save it in a String variable.
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.
Examples
Your program is run 5 times: the 2 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.
two words, printed in the opposite order (rule 1)
You type
- typed: Hello
- typed: World
It prints
World Hello
another pair, swapped (rule 1)
You type
- typed: First
- typed: Second
It prints
Second First