Reverse Each Word of a Sentence Individually
Reverse the letters of each word in a sentence while keeping the word order unchanged.
Split the sentence into words, reverse the letters of each word on its own, then join the reversed words back in the same order. The word sequence stays the same while every word is spelled backward. This runs in O(n) time.
Problem Statement
Given a sentence, reverse the letters of each word individually while keeping the words in their original order. Unlike reversing the whole sentence, the sequence of words does not change — only the characters inside each word are flipped.
For example, Java is fun becomes avaJ si nuf.
Input: A single string s representing a sentence.
Output: The sentence with each word's letters reversed, word order kept.
Examples
Input: "Java is fun"
Output: avaJ si nufJava -> avaJ, is -> si, fun -> nuf, in the same order.
Input: "hello world"
Output: olleh dlrowEach word is reversed on its own; the two words stay in place.
Constraints
0 <= s.length <= 10^5Word order stays the same; only letters within a word reverse
Think Before You Code
Reveal the questions to ask yourself first
- How do you separate the sentence into words to work on each one?
- How do you reverse the characters of a single word?
- How do you keep the words in their original order?
- How do you restore the spaces between the reversed words?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Process the sentence word by word.
- Split the sentence into words on the space character.
- Create a
StringBuilderfor the result. - For each word, wrap it in a
StringBuilder, callreverse(), and append the reversed word to the result. - Append a single space after every word except the last.
- Return the result.
Because the words are appended in their original order, only the letters inside each word are reversed, not the arrangement of the words themselves.
Dry Run
Walk through the example step by step
Reversing each word of "Java is fun":
words: ["Java", "is", "fun"]
i | word | reversed | result so far
--+------+----------+--------------
0 | Java | avaJ | "avaJ"
1 | is | si | "avaJ si"
2 | fun | nuf | "avaJ si nuf"
final: "avaJ si nuf"
Solution
Reveal the full Java solution
public class ReverseEachWord {
public static String reverseEachWord(String s) {
String[] words = s.split(" ");
StringBuilder result = new StringBuilder();
for (int i = 0; i < words.length; i++) {
StringBuilder word = new StringBuilder(words[i]);
result.append(word.reverse());
if (i < words.length - 1) {
result.append(" ");
}
}
return result.toString();
}
public static void main(String[] args) {
System.out.println(reverseEachWord("Java is fun")); // avaJ si nuf
System.out.println(reverseEachWord("hello world")); // olleh dlrow
}
}
Splitting on the space character gives one array entry per word. Each word is
reversed independently with StringBuilder.reverse(), and the reversed words are
appended in the same order they appeared, so the word sequence is untouched. The
index guard adds a separating space between words without leaving one at the end.
O(n) where n is the string lengthSpace: O(n) for the words and outputCommon Mistakes
- Reversing the whole sentence, which also flips the order of the words.
- Appending a trailing space after the final word.
- Reversing the word array as well, changing the word order by accident.
Edge Cases to Test
- A single word returns that word reversed.
- An empty string returns an empty string.
- A palindrome word like "level" comes back unchanged.
Interview Follow-Ups
- How would you reverse each word without using StringBuilder.reverse()?
- How would you keep punctuation attached to the end of a word in place?
- How would you also reverse the order of the words at the same time?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →