Write a method
leetSpeak
that accepts two parameters: a Scanner representing an input file, and a PrintStream representing an output file. Your method should convert the input file's text to "leet speak" (aka 1337 speak), an internet dialect where various letters are replaced by other letters/numbers. Output the leet version of the text to the given output file. Preserve the original line breaks from the input. Also wrap each word of input in parentheses. Perform the following replacements:For example, if the input file
lincoln.txt
contains the following text:four score and seven years ago our
fathers brought forth on this continent a new nation And your method is called in the following way:
Scanner input = new Scanner(new File("lincoln.txt")); PrintStream output = new PrintStream(new File("leet.txt")); leetSpeak(input,output);
Then after the call, the output file leet.txt should contain the following text:
(f0ur) (sc0r3) (4nd) (s3v3n) (y34rZ) (4g0) (0ur) (f47h3rZ) (br0ugh7) (f0r7h) (0n) (7hiZ) (c0n7in3n7) (4) (n3w) (n47i0n)
You may assume that each token from the input file is separated by exactly one space.
Hint: You may want to use the String object's replace method, which is used as follows:
String str = "mississippi"; str = str.replace("s", "*"); // str = "mi**i**ippi"
I'm looking for general feedback, and other ways to accomplish the task of solving this problem. My hope is that there will be some interesting feedback on this that I can apply to my future coding. If there are any bugs in my code, please let me know.
public static void leetSpeak (Scanner sc, PrintStream ps) {
while (sc.hasNextLine()) {
String line = new String(sc.nextLine());
Scanner tokenScanner = new Scanner(line);
while (tokenScanner.hasNext()) {
String token = new String(tokenScanner.next());
token = token.replace("o", "0");
token = token.replace("l", "1");
token = token.replace("e", "3");
token = token.replace("a", "4");
token = token.replace("t", "7");
if (token.substring(token.length() - 1).equals("s")) {
token = new String(token.substring(0, token.length() - 1));
token += "Z";
}
ps.print("(" + token + ")" + " ");
}
ps.println();
}
}
String replace(char oldChar, char newChar)
If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. There are only a few minor differences between my code and your link to another person's code; mainly that in the code you linked there's redundant checking if the string contains that substring. \$\endgroup\$