Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you think you have some skills to help others

help someone and become a part of our community - List Q & A

Java - reverse words in a given string

0 contributions
10 points

1. Overview

In this post we will take a look how to reverse words in given string in java.

Example 1:

String original = "Reverse words in string"; // original
String expected = "string in words Reverse"; // reversed words

Example 2:

String original = "reverse this string in java"; // original
String expected = "java in string this reverse"; // reversed words

2. Reverse words in string

public class JavaReverseWordsInStringExample1 {

    public static void main(String[] args) {

        String original = "Reverse words in string";
        String[] split = original.split(" ");
        StringBuilder builder = new StringBuilder();

        for (int i = split.length - 1; i >= 0; i--) {
            String word = split[i];

            if (i != 0) {
                builder.append(" ");
        String reversedWords = builder.toString();

        System.out.println("# original: ");
        System.out.println("# reversed words: ");


# original: 
Reverse words in string
# reversed words: 
string in words Reverse


0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more