Reputation: 37
The task is quite simple:
You have just been hired by the CIA as a programmer in the encryption department. Your job is to write a class called Crypto. One method, encrypt, will accept a String that represents the sentence to be encrypted. It will return a String that is the sentence with all v’s (big or small) replaced with “ag’,r”, all m’s (big or small) with “ssad”, all g’s (big or small) with “jeb..w”, and all b’s (big or small) with “dug>?/”.
The class contains another method, decrypt, that accepts a String that represents the sentence to be decrypted. In this method the reverse process described above is performed. It returns a String that is the original sentence before encryption.
My encrypt and decrypt methods work completely fine. It's just that it doesn't encrypt/decrypt how it's supposed to. Here's what a sample test run should look like:
Enter a sentence that is to be encrypted: *This is a very big morning.*
Original sentence = This is a very big morning.
Encrypted sentence = This is a ag',rery dug>?/ijeb..w ssadorninjeb..w.
Decrypted sentence = This is a very big morning.
And here's what my output looks like:
Enter a sentence that is to be encrypted: This is a very big morning.
Original sentence = This is a very big morning.
Encrypted sentence = This is a ajedug>?/..w',rery dug>?/ijedug>?/..w ssadorninjedug>?/..w.
Decrypted sentence = This is a very big morning.
As you can tell the encrypted sentence doesn't quite match up, and I know why, I just don't know how to fix it.
Here are my methods:
String encrypt(String string) {
return string
.replace("v", "ag',r").replace("V", "ag',r")
.replace("m", "ssad").replace("M", "ssad")
.replace("g", "jeb..w").replace("G", "jeb..w")
.replace("b", "dug>?/").replace("B", "dug>?/");
}
String decrypt(String string) {
return string
.replace("dug>?/", "b")
.replace("jeb..w", "g")
.replace("ssad", "m")
.replace("ag',r", "v");
}
Any help/guidance is appreciated!
Upvotes: 0
Views: 1902
Reputation: 37
Thank you all for the help! Here's my final (working) code:
String encrypt(String string) {
String s = "";
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
if (c == 'v' || c == 'V') {
s += "ag',r";
} else if (c == 'm' || c == 'M') {
s += "ssad";
} else if (c == 'g' || c == 'G') {
s += "jeb..w";
} else if (c == 'b' || c == 'B') {
s += "dug>?/";
} else {
s += c;
}
}
return s;
}
Upvotes: 0
Reputation: 2458
Other method could be (assuming there will be no occurrences of {1}
, {2}
, {3}
, {4}
markers in the original text) to do something like this:
static String encrypt(String string) {
String encryptedWithNumbers = string
.replace("v", "{1}")
.replace("V", "{1}")
.replace("m", "{2}")
.replace("M", "{2}")
.replace("g", "{3}")
.replace("G", "{3}")
.replace("b", "{4}")
.replace("B", "{4}");
return encryptedWithNumbers
.replace("{1}", "ag',r")
.replace("{2}","ssad")
.replace("{3}", "jeb..w")
.replace("{4}", "dug>?/");
}
Upvotes: 0
Reputation: 273178
Your output is wrong because you are replacing v
with ag',r
, but then replacing the g
in ag',r
with jeb..w
. You should not replace what is already replaced.
To fix this, you need to start with an empty string, and check each character individually. If it is a v
, m
, g
, or b
, you append the corresponding gibberish to the string. If it is none of those letters, you append that letter.
Here, I did just that with a StringBuilder
:
private static String encrypt(String s) {
StringBuilder builder = new StringBuilder();
s.chars().forEach(x -> {
char c = (char)x;
if (c == 'v' || c == 'V') {
builder.append("ag',r");
} else if (c == 'm' || c == 'M') {
builder.append("ssad");
} else if (c == 'g' || c == 'G') {
builder.append("jeb..w");
} else if (c == 'b' || c == 'B') {
builder.append("dug>?/");
} else {
builder.append(c);
}
});
return builder.toString();
}
Edit:
The task specifically says to use string methods and not any external classes
In that case you can't use a StringBuilder
and has to use a String
, but it's the same idea. You just use +=
to append to it.
You also probably can't use forEach
either. You need a normal for loop that loops through the indices of the string.
Upvotes: 1