All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Reverse Words In A String Java

Last Updated : Sep 6, 2023

Reverse Words In A String Java

In this article we will show you the solution of reverse words in a string java, reversing the order of words in a sentence can be useful in various applications, such as text processing and data analysis.

We will cover an efficient approach to solve this problem step by step, providing detailed explanations and example code.

To reverse words in a string, we will follow these steps:

  • Split the input string into an array of individual words.
  • Iterate over the array in reverse order.
  • Append each word to a new string, separated by a space.
  • Return the reversed string as the output.

Step By Step Guide On Reverse Words In A String Java :-

public class StringReversal {
    public static String reverseWords(String input) {
        // Step 1: Split the input string into words
        String[] words = input.split("\\s+");
        // Step 2 and 3: Iterate over the words array in reverse order and append to a new string
        StringBuilder reversedString = new StringBuilder();
        for (int i = words.length - 1; i >= 0; i--) {
            if (i > 0) {
                reversedString.append(" ");
        // Step 4: Return the reversed string
        return reversedString.toString();
    public static void main(String[] args) {
        String input = "Hello World! This is a sample string.";
        String reversed = reverseWords(input);
        System.out.println("Reversed string: " + reversed);
  1. The `reverseWords` method takes the input string as a parameter and splits it into an array of words using the `split("\\s+")` method. The regular expression "\\s+" is used to split the string by one or more whitespace characters.
  2. We create a `StringBuilder` named `reversedString` to store the reversed string.
  3. We iterate over the `words` array in reverse order using a `for` loop, starting from the last element (`words.length - 1`) and ending at the first element (`0`).
  4. Inside the loop, we append each word to the `reversedString` using the `append()` method. If the word is not the last word, we append a space after it to separate the words.
  5. After the loop finishes, we convert the `StringBuilder` to a `String` using the `toString()` method and return the reversed string.
  6. In the `main` method, we provide an example input string, call the `reverseWords` method, and print the reversed string to the console.

Conclusion :-

In this tutorial, we have learned how to reverse words in a string using Java.

By splitting the string into individual words, iterating over them in reverse order, and appending them to a new string, we can effectively reverse the word order.

This technique can be valuable when working with text data in applications such as natural language processing, data analysis, and string manipulation.

By following the step-by-step explanation and utilizing the provided code, you can easily implement word reversal in your Java programs.

I hope this article on reverse words in a string java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪