- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordprocessor.java
More file actions
Latest commit
41 lines (35 loc) · 1.25 KB
/
Copy pathwordprocessor.java
File metadata and controls
41 lines (35 loc) · 1.25 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
importjava.io.*;
importjava.util.*;
classwordprocessor {
publicstaticvoidmain (String[] args) throwsIOException{
BufferedReaderf = newBufferedReader(newFileReader("word.in"));
PrintWriterout = newPrintWriter(newBufferedWriter(newFileWriter("word.out")));
StringTokenizernums = newStringTokenizer(f.readLine());
intn = Integer.parseInt(nums.nextToken());
intk = Integer.parseInt(nums.nextToken());
StringTokenizerst = newStringTokenizer(f.readLine());
intsoFar = 0;
Stringline = "";
for (inti = 0; i < n; i++) {
Stringnext = st.nextToken();
//System.out.println(next);
if (soFar + next.length() <= k) { // there is space for this word
soFar += next.length();
line += next + " ";
if (i == n-1) {
out.println(line.substring(0, line.length()-1));
}
} else { // if too big
out.println(line.substring(0, line.length()-1)); //print previous line
line = "";
soFar = 0;
soFar += next.length();
line += next + " ";
if (i == n-1) {
out.println(line.substring(0, line.length()-1));
}
}
}
f.close(); out.close();
}
}