How to Reverse words in String Java? [Solution]

Hello guys, if you are wondering how to reverse words in a given String in Java then you have come to the right place. you will learn how to reverse words in String. It’s also one of the popular coding questions, so you will also learn how to take a requirement, how to fill gaps in the requirement by asking the right question. A String is nothing but a sentence, which may contain multiple works, or just contain a single word or it may be empty. Your program must produce a String that contains the word in reverse order, for example, if the given input is “Java is Great” then your program should return “Great is Java”.  

Now, if you are a good programmer then you should have some right questions for the Interviewer. Never assume you know everything, even if it looks like a simple problem. Always remember “Devil is in detail”. Also asking a question not only fill the gaps in requirement but also help you to make an impression.

One question the candidate should definitely ask is, what constitutes a word here? For the purpose of this program, the word is nothing but a sequence of non-space characters. Another good question you can ask to Interview is about input like is it possible for input string to contain leading or trailing spaces?

Yes, it’s possible. However, your reversed string should not any contain leading or trailing spaces. One more important question for the Interviewer is about spacing between words, is it possible to have multiple spaces between two words? Yes, it could be possible but you can reduce them to a single space in the reversed string.

Reversing the order of words in a Sentence in Java – Solution

Here is our Java solution to this problem. It’s simple and straightforward.  In this code example, I have shown two ways to reverse words in a String, first one is using, Java’s regular expression support to split the string into spaces and then using the reverse() method of Collections utility class. 
Once you split the String using regex “\\s”, it will return you an array of words. It will also handle words separated by multiple spaces, so you don’t need to worry.

Once you got the array, you can create an ArrayList from an array, and then you are eligible to use Collections.reverse() method. This will reverse your ArrayList and you will have all the words in reverse order, now all you need to do is concatenate multiple String by iterating over ArrayList.

I have used StringBuilder for concatenating String here. Also make sure to specify size, because resizing of StringBuilder is costly as it involves the creation of a new array and copying content from the old to the new array.

The second method to reverse words in a given string is, even more, easier, instead of using the Collections.reverse() method, I have just used the traditional for loop and started looping over array from the end and performing String concatenation. 
This way, you even don’t need to convert your String array to ArrayList of String. This solution is more memory efficient and faster than the previous one.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
 * Java Program to reverse words in String. There are multiple way to solve this
 * problem. you can either use any collection class like List and reverse the
 * List and later create reverse String by joining individual words.
 * @author Javin Paul
public class Testing {
  public static void main(String args[]) {
  * Method to reverse words in String in Java
  public static String reverseWords(String sentence) {
    List< String> words = Arrays.asList(sentence.split("\\s"));
    StringBuilder sb = new StringBuilder(sentence.length());
    for (int i = words.size() - 1; i >= 0; i--) {
      sb.append(' ');
    return sb.toString().trim();
  public static String reverseString(String line) {
    if (line.trim().isEmpty()) {
      return line;
    StringBuilder reverse = new StringBuilder();
    String[] sa = line.trim().split("\\s");
     for (int i = sa.length - 1; i >= 0; i--) {
        reverse.append(' ');
     return reverse.toString().trim();

Sometimes Interviewer may ask you to solve this problem without using the Java Collection framework because it obviously makes the task a lot easier. So it’s also good to prepare a solution based upon pure programming logic. If you know how to reverse an array in Java, then you have an advantage because String is nothing but a character array, but the tricky part is you don’t need to reverse array but to reverse words.

1 thought on “How to Reverse words in String Java? [Solution]”


Leave a Comment

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

error: Content is protected !!