Scientists have found that a text can still be read even if only the first and last letter of a word are in their right place. The remaining characters within the word can be arranged randomly.
Suppose you want to send a text whose content is checked for specific words by a filter. Instead of encrypting it, one could also use this algorithm. But actually that's just a fun program.
I would like to know how professionals rate my code. Can you write parts of the source code more readable?
Example Output
Sntecistis hvae fnuod taht a txet can sltil be read eevn if only the fsrit and lsat
lteter of a wrod are in teihr rghit plcae. The rinnimaeg caartcehrs witihn the wrod
can be araergnd rdnlomay.
Sppuose you want to sned a text wsohe cnteont is cehcekd for seipcfic wdros by a
ftelir. Intsead of enintrpcyg it, one cuold aslo use this aorltihgm. But aallucty
that's jsut a fun prraogm.
I would like to know how pnarloofesiss rtae my code. Can you wtrie ptars of the
suorce cdoe mroe rabdleae?
Another Example
iormpt jvaa.uitl.Scenanr;
imorpt java.io.Flie;
improt jvaa.io.FdeliRaeer;
import jvaa.io.BfeaeudfeRrder;
ipomrt java.io.IxptiEeOcon;
pilubc cslas Main {
pilubc siattc viod main(String[] args) {
SignldetrBuir text = new SeuitdrBginlr();
if (agrs.lgenth == 0) {
Senacnr scennar = new Snenacr(Syestm.in);
text.aeppnd(snncear.niLxtnee());
} esle if (args[0].conatnis("-help")) {
Stysem.out.pirlntn("Jsut clal it lkie taht: java Main");
Seytsm.out.ptnlirn("Or povirde a txt-file: java Main file.txt");
Sestym.exit(0);
} esle {
// read whloe file in Sntirg
File file = new File(args[0]);
try (BeafedurfRdeer br = new BerfuReeadedfr(new FadleeReir(flie))) {
Snritg lnie;
wihle ((line = br.rLniadee()) != null) {
text.apnepd(lnie + "\n");
}
} ccath (IOtExeocipn e) {
e.pacttSTcnrrkiae();
}
}
TeuCxeosfntr tc = new TsCxtofenuer(txet.totnriSg());
tc.cnufsoe();
Setsym.out.priltnn(tc.gTdoxseCfneutet());
}
}
And here is the source code
Main.java
import java.util.Scanner;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
StringBuilder text = new StringBuilder();
if (args.length == 0) {
Scanner scanner = new Scanner(System.in);
text.append(scanner.nextLine());
} else if (args[0].contains("-help")) {
System.out.println("Just call it like that: java Main");
System.out.println("Or provide a txt-file: java Main file.txt");
System.exit(0);
} else {
// read whole file in String
File file = new File(args[0]);
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
text.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
TextConfuser tc = new TextConfuser(text.toString());
tc.confuse();
System.out.println(tc.getConfusedText());
}
}
Text Confuser.java
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
public class TextConfuser {
private String text;
private List<String> words;
private StringBuilder confusedText;
public TextConfuser(String text) {
this.text = text;
this.words = new ArrayList<String>();
this.confusedText = new StringBuilder();
}
public void setText(String text) {
this.text = text;
this.confusedText = new StringBuilder();
}
public String getText() {
return text;
}
public String getConfusedText() {
return confusedText.toString();
}
// make text to a list of words for further processing
public void parse() {
// a word can be a word like you know it from natural language
// special symbols like punctuation characters and control characters are treated as own words
words = new ArrayList<String>();
char[] characters = text.toCharArray();
StringBuilder currentWord = new StringBuilder();
for (int i = 0; i < characters.length; i++) {
if (Character.isLetter(characters[i])) {
currentWord.append(characters[i]);
} else {
if (currentWord.length() > 0) {
words.add(currentWord.toString());
currentWord = new StringBuilder();
}
words.add(Character.toString(characters[i]));
}
}
StringBuilder result = new StringBuilder();
for (String word : words) {
result.append(word);
}
}
public void confuse() {
parse();
for (String word : words) {
if (word.length() > 3) {
// get first and last character
char firstChar = word.charAt(0);
char lastChar = word.charAt(word.length() - 1);
// get list of characters between first and last character
List<Character> chars = new ArrayList<>();
for (int i = 1; i < word.length() - 1; i++) {
chars.add(word.charAt(i));
}
// construct confused word
StringBuilder confusedWord = new StringBuilder();
confusedWord.append(firstChar);
while (!chars.isEmpty()) {
Random random = new Random();
Character randomChar = chars.get(random.nextInt(chars.size()));
chars.remove(randomChar);
confusedWord.append(randomChar);
}
confusedWord.append(lastChar);
confusedText.append(confusedWord);
} else {
confusedText.append(word);
}
}
}
}