ReplaceBrace [source code]
```java
public class ReplaceBrace { public static void main (String[] args) { Scanner scan = new Scanner (System.in); String line = ""; while ((line = scan.nextLine ()) != null && !line.equals ("start")) { // change to while for repeating stdin fetching line = line.replace ("[", "{").replace ("]", "}"); System.out.println (wrap (line)); } String pair = ""; while ((pair = scan.nextLine ()) != null) { String[] tokens = pair.split ("\s+"); if (tokens.length != 2) continue; line = line.replace (parse_escape (tokens[0]), parse_escape (tokens[1])); // System.out.printf ("%s -> %s\n", parse_escape (tokens[0]), parse_escape (tokens[1])); System.out.println (wrap (line)); } }
static String wrap (String s) {
int code = 35;
// return (char) 27 + '[' + code + 'm' + s + (char) 27 + "[0m"; // wrong implementation: char is dangerous
return (char) 27 + "[" + code + "m" + s + (char) 27 + "[0m";
}
static String parse_escape (String s) {
int index;
return s.replaceAll ("\\\\n", "\n").replaceAll ("\\\\t", "\t");
}
}