-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseWords.java
More file actions
44 lines (41 loc) · 1.17 KB
/
reverseWords.java
File metadata and controls
44 lines (41 loc) · 1.17 KB
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main (String[] args) {
File file = new File(args[0]);
String temp = "";
String result = "";
int start = -1;
int end = -1;
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
while((line = in.readLine()) != null){
String[] inputString = line.split("[ ]+");
start = 0;
end = inputString.length-1;
while(start < end){
temp = inputString[start];
inputString[start] = inputString[end];
inputString[end] = temp;
start++;
end--;
}
result = inputString[0];
for(int i = 1; i < inputString.length; i++){
result += " "+inputString[i];
}
System.out.println(result);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}