Kodekraftt
/Blog
/Reverse Words In A Sentence 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 Words in a Sentence in Java — Definition, Examples and Program

Learn how to reverse each word in a sentence in Java while keeping the word order intact, using a character-based approach without advanced string libraries.

Reverse Words in a Sentence in Java

Reversing words in a sentence means reversing the characters of each word individually while keeping the original order of words unchanged.

This type of string manipulation is frequently asked in interviews and is useful for understanding character-level string processing.

Understanding the Problem

We do not reverse the entire sentence. Instead, each word is reversed at its own place.

Example 1

Input: Java is fun

Output: avaJ si nuf

Example 2

Input: Hello World

Output: olleH dlroW

Logic Explanation

  1. Traverse the string character by character.
  2. Store characters of a word temporarily.
  3. When a space is encountered, reverse the stored word.
  4. Append the reversed word to the result.
  5. Repeat until the end of the string.

Java Program to Reverse Words in a Sentence


import java.util.Scanner;

public class ReverseWordsInSentence {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a sentence: ");
        String str = sc.nextLine();

        String word = "";
        String result = "";

        for (int i = 0; i < str.length(); i++) {

            char ch = str.charAt(i);

            if (ch != ' ') {
                word = ch + word; // reverse the word
            } else {
                result = result + word + " ";
                word = "";
            }
        }

        // add last word
        result = result + word;

        System.out.println("Reversed words sentence: " + result);
    }
}

Sample Output


Enter a sentence: Java is fun
Reversed words sentence: avaJ si nuf

Key Points

  • Word order remains unchanged.
  • Each word is reversed individually.
  • No use of split() or StringBuilder.reverse().

Practice Challenges

  1. Reverse the order of words instead of characters.
  2. Handle multiple spaces between words.
  3. Rewrite the program using StringBuilder.

This program helps strengthen understanding of strings, loops, and conditional logic in Java.

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.