String[] words = sentence.split(" ");
String[] reversedWords = ArrayUtils.reverse(words);
String reversedSentence = StringUtils.join(reversedWords, " ");

(using ArrayUtils and StringUtils from commons-lang, but these are easy methods to write - just a few loops)

Answer from Bozho on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ reverse-words-given-string-java
Reverse words in a given String in Java - GeeksforGeeks
January 22, 2026 - Input: "Welcome to geeksforgeeks" ... between words and at the start or end of the string ยท Step 1: Split the string into words using a regex pattern for whitespace....
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ reverse-words-in-a-string
Reverse Words in a String - LeetCode
Can you solve this real interview question? Reverse Words in a String - Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters.
Discussions

java - Reversing some words in a string - Stack Overflow
I need to reverse 5 or more character long words in a given string. For example: * Given string: My name is Michael. * Output: My name is leahciM. Rest of the sentence stays the same, just those... More on stackoverflow.com
๐ŸŒ stackoverflow.com
15 min Java Coding Challenge - Reverse Words in a String on YouTube. My eyes are fucking bleeding after seeing this code.
It's not bad, but it's as if he forgot he was writing Java and not C. More on reddit.com
๐ŸŒ r/programminghorror
130
239
April 17, 2018
Return the reversed words in a string, while preserving the word order
Razvan Cirstea is having issues with: Hello, I am stuck trying to solve the following exercise: Given a string, you need to reverse the order of characters in each w... More on teamtreehouse.com
๐ŸŒ teamtreehouse.com
2
November 24, 2017
Java Program to reverse individual words in the sentence - Stack Overflow
This won't work if . char is in the middle of String. str.contains(".") will be true for He.llo also 2020-02-02T06:00:46.163Z+00:00 ... Yes, i modified for test case mentioned here. Although u can keep the index of dot and after reverse of word append dot at the index. 2020-02-02T06:06:51.007Z+00:00 ... Here is the java ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ practicing-string-operations-and-type-conversions-in-java โ€บ lessons โ€บ string-manipulation-splitting-and-reversing-words-in-java
String Manipulation: Splitting and Reversing Words in Java
Afterward, it forms a single string with these reversed words, producing "olleH taen 321_srevol_avaj". Therefore, if you call reverseWords("Hello neat java_lovers_123"), the function should return "olleH taen 321_srevol_avaj".
๐ŸŒ
AlgoCademy
algocademy.com โ€บ link
Java - Reverse Words in a String
Use the split() method to divide the input string into an array of words. Reverse the array of words.
Find elsewhere
๐ŸŒ
Medium
rameshfadatare.medium.com โ€บ java-program-to-reverse-each-word-of-a-string-0da9e728702d
Java Program to Reverse Each Word of a String | by Ramesh Fadatare | Medium
December 13, 2024 - Reassemble the String: Combine the reversed words back into a single string. Display the Result: Print the string with each word reversed. Close Resources: Close the Scanner class object automatically using the try-resource statement. // Java Program to Reverse Each Word of a String import java.util.Scanner; public class ReverseWordsInString { public static void main(String[] args) { // Step 1: Read the string from the user try (Scanner scanner = new Scanner(System.in)) { System.out.print("Enter a string: "); String input = scanner.nextLine(); // Step 2: Split the string into words String[] wo
๐ŸŒ
CodeChef
codechef.com โ€บ practice โ€บ course โ€บ strings โ€บ STRINGS โ€บ problems โ€บ PALINDRCHECK
Reverse Words in a String Practice Problem in Strings
Test your knowledge with our Reverse Words in a String practice problem. Dive into the world of strings challenges at CodeChef.
๐ŸŒ
HowToDoInJava
howtodoinjava.com โ€บ home โ€บ java examples โ€บ java program to reverse words in string without using functions
Java program to reverse words in string without using functions
November 13, 2022 - Original string : how to do in java Reversed string : java in do to how ยท In this example, we will follow below steps to reverse the characters of each word in itโ€™s place. Read string from input. Split the string by using space as delimiter.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ reverse-words-in-a-given-string
Reverse words in a string
Reverse the entire string, then iterate through it to extract words separated by dots. Reverse each word individually and update the original string until the end is reached.
Published ย  May 3, 2010
๐ŸŒ
Reddit
reddit.com โ€บ r/programminghorror โ€บ 15 min java coding challenge - reverse words in a string on youtube. my eyes are fucking bleeding after seeing this code.
r/programminghorror on Reddit: 15 min Java Coding Challenge - Reverse Words in a String on YouTube. My eyes are fucking bleeding after seeing this code.
April 17, 2018 - For example "This is the input" becomes "sihT si eht tupni", not just a complete reversal ... I think you've got it slightly backwards; "This is the input" should become "input the is This". But if I recall correctly, the easiest way to do this is to reverse the entire string, then re-reverse each individual word.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-reverse-each-word-of-a-string-in-Java
How to reverse each word of a string in Java - Quora
Answer (1 of 2): Hi, We can reverse each word of a string by the help of reverse(), split() and substring() methods. By using a reverse() method of StringBuilder class, we can reverse given string. By the help of split("\\s") method, we can get all words in an array. To get the first character, ...
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_howto_reverse_string.asp
Java How To Reverse a String
Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 60023226 โ€บ java-program-to-reverse-individual-words-in-the-sentence
Java Program to reverse individual words in the sentence - Stack Overflow
Hello Java."; String collect = Arrays.stream(str.split("\\s+")) // split sentences into words .map(s -> new StringBuffer(s)) // converting to StringBuffer .map(s -> s.reverse()) // reversing the string .map(s -> replaceChar(s)) // replace first ...
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2017 โ€บ 09 โ€บ java-program-to-reverse-words-in-a-string
Java Program to reverse words in a String
September 15, 2017 - public class Example { public void reverseWordInMyString(String str) { /* The split() method of String class splits * a string in several strings based on the * delimiter passed as an argument to it */ String[] words = str.split(" "); String reversedString = ""; for (int i = 0; i < words.length; i++) { String word = words[i]; String reverseWord = ""; for (int j = word.length()-1; j >= 0; j--) { /* The charAt() function returns the character * at the given position in a string */ reverseWord = reverseWord + word.charAt(j); } reversedString = reversedString + reverseWord + " "; } System.out.println(str); System.out.println(reversedString); } public static void main(String[] args) { Example obj = new Example(); obj.reverseWordInMyString("Welcome to BeginnersBook"); obj.reverseWordInMyString("This is an easy Java Program"); } }
๐ŸŒ
GitConnected
levelup.gitconnected.com โ€บ java-algorithms-reverse-words-in-string-leetcode-b6d180252e28
Java algorithms: Reverse words in a string (LeetCode) | by Ruslan Rakhmedov | Level Up Coding
June 21, 2022 - String in Java is immutable and ... task asks us to reverse the order of words. To solve this we need to iterate through our array of words backwards....
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 484442 โ€บ java โ€บ Reverse-words-string
Reverse the words in a string (Java in General forum at Coderanch)
Once you get your newsentence, you can add a period at the end and replace the first character of the string with an upper case version of it. You can use the split() method, if you are allowed to, to get the words into a String array and read that array backwards to reverse the string.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-reverse-the-words-in-a-string-using-Java
How to reverse the words in a string using Java - Quora
Answer (1 of 31): There are various methods to reverse a string in Java Pseudo Code for Reverse String : 1. Convert the input string into character array by using the toCharArray() built in method of the String Class . 2. In this method we will scan the character array from both sides , that is...