Challenge: Reverse a Word
Completed
Read one word and print it backwards.
Input
One word.
Output
The word reversed, e.g. avaj for java.
Example
Input
java
Output
avaj
Editor
Java
Hints
new StringBuilder(word) makes a StringBuilder holding the word.
StringBuilder has a reverse() method.
Solution explanation
new StringBuilder(word).reverse() flips the characters.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String word = sc.next();
StringBuilder sb = new StringBuilder(word);
System.out.println(sb.reverse());
}
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module