Reverse Each Word in a String in Java
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.
Java Program
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
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.
- 1
str.split(" ")breaks the sentence into an array of individual words. - 2A loop visits each word in the array, in its original order.
- 3
new StringBuilder(words[i]).reverse()flips that single word's characters, leaving the array's order untouched. - 4Each reversed word is appended to the result, with a space added between words but not after the last one.
"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.
Why: split() allocates an array holding every word, and the result buffer accumulates a string proportional to the sentence's length.
Key Concepts
Approach 2: Java 8
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
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.
- 1
Arrays.stream(str.split(" "))turns the split words into aStream<String>. - 2
.map(word -> new StringBuilder(word).reverse().toString())reverses each word individually, the same way the loop version does. - 3
.collect(Collectors.joining(" "))joins the reversed words back together with single spaces between them.
["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.
Why: The stream maps every word to its reversed form, and Collectors.joining() builds a result string holding the whole sentence.