Java Program to Reverse Each Word in a String

This is the Java Program to Reverse a String Word by Word in Place

Problem Description

Given a string, reverse each word in its place, where words are separated by whitespace.

Example:

String x = “We belong to Russia”
Output = “eW gnoleb ot aissuR”

Problem Solution

Split the given string into various substrings, whenever space is encountered. Reverse each substring and create a new string from the reversed substrings.

Program/Source Code

Here is the source code of the Java Program to Reverse a String Word by Word in Place. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

advertisement
  1.  
  2. // Java Program to Reverse a String Word by Word in Place.
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class ReverseWordsInPlace {
  8.     // Function to reverse the string
  9.     static String reverseString(String str) {
  10.         String[] words = str.split(" ");
  11.         String rev = "";
  12.         int i, j;
  13.         for (i = 0; i < words.length; i++) {
  14.             StringBuffer sb = new StringBuffer(words[i]);
  15.             rev+=sb.reverse().toString();
  16.             rev+=" ";
  17.         }
  18.         return rev;
  19.     }
  20.     // Function to read the input and display the output
  21.     public static void main(String[] args) {
  22.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  23.         System.out.println("Enter the text string");
  24.         String str;
  25.         try{
  26.             str=br.readLine();
  27.         }
  28.         catch(Exception e){
  29.             System.out.println("Error reading input");
  30.             return;
  31.         }
  32.         String rev = reverseString(str);
  33.         System.out.println("The reverse of the string word by word in place is\n");
  34.         System.out.println(rev);
  35.     }
  36. }
Program Explanation

1. In function reverseString(), the string str is split into various tokens using split function and the resulting array of tokens is stored in variable words.
2. The loop for (i = 0; i < words.length; i++) traverses through the words array.
3. The nested loop for (j = words[i].length() – 1; j >= 0; j–) traverses the various words in reverse order, adding each character to the rev string.
4. Finally the rev string is displayed.

Time Complexity: O(n) where n is the length of the string.

🎓 Register Today for Free C++ Certification - December 2025
Runtime Test Cases
 
Case 1 (Simple Test Case):
 
Enter the text string
We belong to Russia
The reverse of the string word by word in place is
eW gnoleb ot aissuR
 
Case 2 (Simple Test Case - another example):
 
Enter the text string
Hello I am Jane
The reverse of the string word by word in place is
olleH I ma enaJ

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.