-
Notifications
You must be signed in to change notification settings - Fork 14
/
ReverseWords.java
48 lines (43 loc) · 1.3 KB
/
ReverseWords.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Reverse Words
* ---------------
* @author adambose1990
*
* CHALLENGE DESCRIPTION:
* Write a program which reverses the words in an input sentence.
*
* INPUT SAMPLE:
* The first argument is a file that contains multiple sentences, one per line. Empty lines are also possible.
* For example:
* Hello World
* Hello CodeEval
*
* OUTPUT SAMPLE:
* Print to stdout each sentence with the reversed words in it, one per line. Empty lines in the input should be ignored.
* Ensure that there are no trailing empty spaces in each line you print.
* For example:
* World Hello
* CodeEval Hello
*/
package com.codeeval.easy;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ReverseWords {
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
File file = new File("files/easy/test_ReverseWords.txt");
BufferedReader buffer = new BufferedReader(new FileReader(file));
String line;
while ((line = buffer.readLine()) != null) {
line = line.trim();
String[] words = line.split(" ");
StringBuffer buff = new StringBuffer();
for (int i = words.length - 1; i >= 0; i--) {
buff.append(words[i]).append(" ");
}
System.out.println(buff.substring(0, buff.length() - 1));
}
}
}