Java Program to Reverse Each Word in a String

In this post we’ll see a Java program to reverse each word in a String individually rather than reversing the whole string.

Steps to reverse each word in a String

In order to write a Java program to reverse each word in a String you can follow the steps as given below-

  1. Split the passed String using split() method, that gives you an array having all the words in a String.
  2. Iterate the array, taking one word at a time and reverse it. For reversing you can write your own logic using both recursive and non-recursive methods or use reverse method of the StringBuilder() class.
  3. Append each reversed word to a string.

Reverse each word in a String using recursion – Java Program

Output

Reverse each word in a String non-recursive – Java Program

Output

Using reverse() method of StringBuilder class

Output

That’s all for the topic Java Program to Reverse Each Word in a String. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.