我正在尝试解密Vigenere_Cipher
当我输入BEXR TKGKTRQFARI
时,输出为JAVAPROGRAMMING
但我想要
放置JAVA PROGRAMMING
之类的空间。
我的代码
public static String VigenereDecipher(String text) {
String keyword = "SECRET";
String decipheredText = "";
text = text.toUpperCase();
for (int i = 0, j = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c < 'A' || c > 'Z') continue;
decipheredText += (char)((c - keyword.charAt(j) + 26) % 26 + 'A');
j = ++j % keyword.length();
}
return decipheredText;
}
答案 0 :(得分:1)
您明确地忽略了空格。您只需要添加以下行:
if (c == ' ') {
decipheredText += ' ';
}
请确保将其放在此行之前:
if (c < 'A' || c > 'Z') continue;
答案 1 :(得分:0)
您正在忽略空格。在检查字符范围“ A”到“ Z”时检查空间,仅当您不希望将空间视为另一个字符时,才将其作为空格添加到decipheredText中。