Problem statement:
Write a java program to reverse a string entered by the user.
Input:
A string, for example “hello”
Output:
The reversed string “olleh”
Explanation (Very simple language):
Image you’re reading a word backward, like reading from the last letter to the first letter. So instead of:
h->e->l->l->o
We go:
o->l->l->e->h
That’s what reversing a string means — flipping it.
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
// Step 1: Ask the user to enter a word
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a word: ");
String input = scanner.nextLine();
// Step 2: Create a new string to store the reversed word
String reversed = "";
// Step 3: Loop from last letter to the first
for (int i = input.length() - 1; i >= 0; i--) {
reversed = reversed + input.charAt(i); // Add each letter one by one
}
// Step 4: Print the result
System.out.println("Reversed word is: " + reversed);
}
}
Step-by-Step
- We use scanner to take input from the user.
- We create an empty string reversed to keep our flipped word.
- We run a loop from the last letter to the first, using charAt(i) to get each letter.
- We add each letter to the reversed string.
- We show the final reversed word.
Example:
If the user types: “apple”
The output will be: “elppa”
Challange For You (Try this):
Can you reverse the string without using a loop? Hint: Check out StringBuilder in Java.

