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));
    }/*from  w  w w .j a v a  2s  .c om*/

    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