The Tech Platform

Dec 8, 20221 min

How to reverse each word of a string in Java?

Split the given string into various substrings, whenever space is encountered. Reverse each substring and create a new string from the reversed substrings. This tutorial will show How to reverse each word of a string in Java.

See the below code:

Code:

import java.io.BufferedReader;
 
import java.io.InputStreamReader;
 

 
public class ReverseWordsInPlace {
 
// Function to reverse the string
 
static String reverseString(String str) {
 
String[] words = str.split(" ");
 
String rev = "";
 
int i, j;
 
for (i = 0; i < words.length; i++) {
 
StringBuffer sb = new StringBuffer(words[i]);
 
rev+=sb.reverse().toString();
 
rev+=" ";
 
}
 
return rev;
 
}
 
// Function to read the input and display the output
 
public static void main(String[] args) {
 
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
System.out.println("Enter the string : ");
 
String str;
 
try{
 
str=br.readLine();
 
}
 
catch(Exception e){
 
System.out.println("Error reading input");
 
return;
 
}
 
String rev = reverseString(str);
 
System.out.println("The reverse of the string is :");
 
System.out.println(rev);
 
}
 
}

Output:

The Tech Platform

www.thetechplatform.com

    0