Java ProgramsStringsReverse Each Word in a String

Reverse Each Word in a String in Java

intermediate·  Strings  ·  String Manipulation

Problem

Reversing each word means flipping the letters inside every individual word, while the words themselves stay in their original order — the opposite of reversing the order of the words.

Given a sentence, reverse the letters within each word without changing the order of the words.

Input
Hello World
Output
olleH dlroW

Java Program

Java
public class ReverseEachWord { public static void main(String[] args) { String str = "Hello World"; String[] words = str.split(" "); StringBuilder result = new StringBuilder(); for (int i = 0; i < words.length; i++) { result.append(new StringBuilder(words[i]).reverse()); // reverse just this one word if (i != words.length - 1) result.append(" "); // space between words, but not after the last one } System.out.println(result.toString()); } }

Output

olleH dlroW

Core Logic

Splitting the sentence into words, reversing each one individually, and rejoining them with spaces keeps the word order while flipping each word's letters.

How It Works
  1. 1str.split(" ") breaks the sentence into an array of individual words.
  2. 2A loop visits each word in the array, in its original order.
  3. 3new StringBuilder(words[i]).reverse() flips that single word's characters, leaving the array's order untouched.
  4. 4Each reversed word is appended to the result, with a space added between words but not after the last one.
For "Hello World", "Hello" reverses to "olleH" and "World" reverses to "dlroW", while the two words stay in their original order — producing "olleH dlroW".
💡

Key Point: This is the mirror image of reversing the order of words — here every word's own letters flip, but which word comes first or second never changes.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: split() allocates an array holding every word, and the result buffer accumulates a string proportional to the sentence's length.

Key Concepts

String.split()StringBuilder.reverse()for loop

Approach 2: Java 8

Java
import java.util.Arrays; import java.util.stream.Collectors; public class ReverseEachWordStream { public static void main(String[] args) { String str = "Hello World"; // Reverses each word individually, then joins them with spaces String result = Arrays.stream(str.split(" ")) .map(word -> new StringBuilder(word).reverse().toString()) .collect(Collectors.joining(" ")); System.out.println(result); } }

Output

olleH dlroW

Core Logic

The same per-word reversal can be expressed as a stream — map each word to its reversed form and join the results with spaces.

How It Works
  1. 1Arrays.stream(str.split(" ")) turns the split words into a Stream<String>.
  2. 2.map(word -> new StringBuilder(word).reverse().toString()) reverses each word individually, the same way the loop version does.
  3. 3.collect(Collectors.joining(" ")) joins the reversed words back together with single spaces between them.
Mapping ["Hello", "World"] reverses each word to "olleH" and "dlroW", and joining them gives "olleH dlroW".
💡

Key Point: Collectors.joining(" ") handles the space-between-words bookkeeping automatically, the same convenience it offers when reversing word order instead of word letters.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: The stream maps every word to its reversed form, and Collectors.joining() builds a result string holding the whole sentence.

Key Concepts

StreamArrays.stream()Collectors.joining()

Related Programs