-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseWords.java
More file actions
86 lines (61 loc) · 1.87 KB
/
ReverseWords.java
File metadata and controls
86 lines (61 loc) · 1.87 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
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.
Limits
Small dataset
N = 5
1 ≤ L ≤ 25
Large dataset
N = 100
1 ≤ L ≤ 1000
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 code.google;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class ReverseWords {
public static void main(String ar[]){
try {
BufferedReader read=new BufferedReader(new FileReader("B-large-practice.in"));
int N=Integer.parseInt(read.readLine());
String s="";
BufferedWriter writer = new BufferedWriter(new FileWriter("B-large-practice.txt", true));
for(int i=0;i<N;i++){
s=read.readLine();
StringTokenizer tokenizer=new StringTokenizer(s," ");
String[] word=new String[tokenizer.countTokens()];
int j=0;
while(tokenizer.hasMoreTokens()){
word[j++]=tokenizer.nextToken();
}
StringBuffer st=new StringBuffer(word[j-1]);
for(int k=(j-2);k>=0;k--){
st.append(" "+word[k]);
}
writer.write("Case #"+(i+1)+": "+st.toString());
writer.newLine();
}
writer.close();
} catch (Exception e) {
}
}
}