Problem
Given a list of space separated words, reverse the order of the words.
Each line of text contains L letters and W words.
A line will only consist of letters and space characters.
There will be exactly one space character between each pair of
consecutive words.
Input
The first line of input gives the number of cases, N.
N test cases follow. For each test case there will a line of letters and
space characters indicating a list of space separated words.
Spaces will not appear at the start or end of a line.
Output
For each test case, output one line containing "Case #x: "
followed by the list of words in reverse order.
Sample Input
3
this is a test
foobar
all your base
Output
Case #1: test a is this
Case #2: foobar
Case #3: base your all
package codejam_class;import java.util.Arrays;import java.util.Collections;import java.util.List;import java.util.Scanner;/** * The the sentence or words split them by pieces. Store them in an array. * Reverse the array and output the result as sentence or words. * @author RAYMARTHINKPAD */ public class ReverseWords { /** * Output the result * @param n * @return */ public String result(int n){ String input; Scanner scan = new Scanner(System.in); String res = ""; for(int i = 0; i < n; i++) { int j = i+1; input = scan.nextLine(); res += "\n" + "Case #" + j + ": " + reverseIt(storeStrArry(input)); } return res; } /** * Take the input as sentence alike and splits them if possible removing * spaces and storing elements in an array * @param words * @return */ public String[] storeStrArry(String words) { String[] str = words.split(" "); return str; } /** * Get the input and reverse it joining them as sentence for output * @param str * @return */ public String reverseIt(String[] str) { List<String> list = Arrays.asList(str); Collections.reverse(list); str = (String[]) list.toArray(); String joined = String.join(" ", str); return joined; }}