Java - Write code to reverse Words in a String

Description

Write code to reverse Words in a String

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String input = "this is a test";
        System.out.println(reverseWords(input));
    }/*  w  ww  .  j av  a2  s.co  m*/

    public static String reverseWords(String input) {
        StringBuilder result = new StringBuilder();
        String[] words = input.split(" ");
        for (int i = words.length - 1; i >= 0; i--) {
            result.append(words[i] + (i == 0 ? "" : " "));
        }
        return result.toString();
    }
}

Write code to reverse Words in a String

Related Quiz