Reverse each individual word of "Hello World" string with Java

This should do the trick. This will iterate through each word in the source string, reverse it using StringBuilder's built-in reverse() method, and output the reversed word.

String source = "Hello World";

for (String part : source.split(" ")) {
    System.out.print(new StringBuilder(part).reverse().toString());
    System.out.print(" ");
}

Output:

olleH dlroW 

Notes: Commenters have correctly pointed out a few things that I thought I should mention here. This example will append an extra space to the end of the result. It also assumes your words are separated by a single space each and your sentence contains no punctuation.


Know your libraries ;-)

import org.apache.commons.lang.StringUtils;

String reverseWords(String sentence) {
    return StringUtils.reverseDelimited(StringUtils.reverse(sentence), ' ');
}

You need to do this on each word after you split into an array of words.

public String reverse(String word) {
    char[] chs = word.toCharArray();

    int i=0, j=chs.length-1;
    while (i < j) {
        // swap chs[i] and chs[j]
        char t = chs[i];
        chs[i] = chs[j];
        chs[j] = t;
       i++; j--;
    }
    return String.valueOf(chs);
}

Here's the simplest solution that doesn't even use any loops.

public class olleHdlroW {
    static String reverse(String in, String out) {
        return (in.isEmpty()) ? out :
            (in.charAt(0) == ' ')
            ? out + ' ' + reverse(in.substring(1), "")
            : reverse(in.substring(1), in.charAt(0) + out);
    }
    public static void main(String args[]) {
        System.out.println(reverse("Hello World", ""));
    }
}

Even if this is homework, feel free to copy it and submit it as your own. You'll either get an extra credit (if you can explain how it works) or get caught for plagiarism (if you can't).


No one here is considering unicode characters. You need to use java.text.BreakIterator to find word boundaries and then use another one within each word boundary to enumerate character boundaries:

String helloWorld = "He\u0308llo World"; // Hëllo World
StringBuilder reverseStringBuilder = new StringBuilder(helloWorld.length());
BreakIterator wordBreakIterator = BreakIterator.getWordInstance();
wordBreakIterator.setText(helloWorld);

int wordStart = wordIterator.first();
int wordEnd = wordIterator.next();

while (wordEnd != BreakIterator.DONE) {
    String word = helloWorld.substring(wordStart,wordEnd);
    if (Character.isLetterOrDigit(word.charAt(0))) {
        // "Hello" or "World" in our example
        BreakIterator characterBreakIterator = BreakIterator.getCharacterInstance();
        characterBreakIterator.setText(word);
        int characterEnd = characterBreakIterator.last();
        int characterStart = characterBreakIterator.previous();
        while (characterStart != BreakIterator.DONE) {
            reverseStringBuilder.append(word.substring(characterStart, characterEnd));

            characterEnd = characterStart;
            characterStart = characterBreakIterator.previous();
        }
    } else {
        // " " in our example
        reverseStringBuilder.append(word);
    }
    wordStart = wordEnd;
    wordEnd = wordIterator.next();
}

String dlroWolleh = reverseStringBuilder.toString(); // "dlroW ollëH"

Using naive methods above will shift the diacritic character \u0308 above the first l when you reverse the String. You want it to stay above the e.