Kodekraftt
/Blog
/Reverse A String In Java Definition Examples And Program
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Reverse a String in Java — Definition, Examples and Program

Learn how to reverse a string in Java using a simple and efficient approach. This blog explains the logic step by step with examples, a Java program, and practice challenges.

Reverse a String in Java — Definition, Examples and Program

Reversing a string is one of the most fundamental problems in programming. It helps beginners understand loops, character handling, and string manipulation.

In this blog, we will learn:

  • What it means to reverse a string
  • Step-by-step examples
  • A clean Java program
  • Practice challenges

What Does Reversing a String Mean?

Reversing a string means changing the order of its characters so that the last character becomes the first, the second last becomes the second, and so on.

Example:

  • "hello" → "olleh"
  • "Java" → "avaJ"
  • "12345" → "54321"

How the Logic Works

To reverse a string manually:

  1. Start from the last character of the string.
  2. Move towards the first character.
  3. Print or store each character one by one.

Example 1

Input: "CODE"

  • Start from index 3 → 'E'
  • Index 2 → 'D'
  • Index 1 → 'O'
  • Index 0 → 'C'

Output: "EDOC"

Java Program to Reverse a String


import java.util.Scanner;

public class ReverseString {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a string: ");
        String input = sc.nextLine();

        String reversed = "";

        for (int i = input.length() - 1; i >= 0; i--) {
            reversed = reversed + input.charAt(i);
        }

        System.out.println("Reversed String: " + reversed);
    }
}

Sample Output


Enter a string: programming
Reversed String: gnimmargorp

Practice Challenges

  1. Reverse a string without using an extra variable.
  2. Reverse each word in a sentence separately.
  3. Check whether a string is a palindrome using the reverse logic.

Reversing a string is a common interview question and a great way to build confidence in string manipulation. Mastering this logic will help you solve many advanced problems easily.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.