I want to reverse each individual word of a String in Java (not the entire string, just each individual word).
Example: if input String is “Hello World” then the output should be “olleH dlroW”.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This should do the trick. This will iterate through each word in the source string, reverse it using
StringBuilder‘s built-inreverse()method, and output the reversed word.Output:
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.