-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTranslate.java
31 lines (29 loc) · 1.13 KB
/
Translate.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
import java.io.IOException;
import java.io.StringReader;
/** String translation.
* @author Kiet Lam
*/
public class Translate {
/** Return the String S, but with all characters that occur in FROM
* changed to the corresponding characters in TO. FROM and TO must
* have the same length. */
static String translate(String S, String from, String to) {
/* NOTE: The try {...} catch is a technicality to keep Java happy. */
char[] buffer = new char[S.length()];
try {
StringReader sReader = new StringReader(S);
TrReader trReader = new TrReader(sReader, from, to);
char[] chs = new char[S.length()];
trReader.read(chs, 0, chs.length);
return new String(chs);
} catch (IOException e) { return null; }
}
/*
REMINDER: translate must
a. Be non-recursive
b. Contain only 'new' operations, and ONE other method call, and no
other kinds of statement (other than return).
c. Use only the library classes String, and anything containing
"Reader" in its name (browse the on-line documentation).
*/
}