密码是表示消息的秘密文本。在这个问题中,原始消息中的每个字符都由密码中的6个字符的字符串表示。例如,文本“java”由4个字符组成,在密码中表示为24个字符的字符串:
kjdktizldzaecvyrvwaaemgk
在这个问题中,您的任务是使用下面给出的方法decipherChar(String)来完成解码密码的方法解码(String)。
/**
* This method decodes a 6-character input String into a single character.
* The first and second halves of the input String are compared lexicographically.
* The middle character of the smaller half is then returned.
* In case of equal halves, a space character is returned.
*
* @param inputStr The 6-character String to be decoded.
* @return The decoded character.
*/
public static char decipherChar(String inputStr) {
// implementation not shown here
}
说明
答案 0 :(得分:0)
String returnstring = "";
if(cipher.equals(""))
{
return "no cipher";
}
if(cipher.length() %6!=0)
{
return "invalid cipher";
}
if(cipher.length()==6)
{
char A = decipherChar(cipher.substring(0,6));
returnstring = Character.toString(A);
}
if(cipher.length()==12)
{
char A = decipherChar(cipher.substring(0,6));
char B = decipherChar(cipher.substring(6,12));
returnstring = Character.toString(A) + Character.toString(B) ;
}
if(cipher.length()==18)
{
char A = decipherChar(cipher.substring(0,6));
char B = decipherChar(cipher.substring(6,12));
char C = decipherChar(cipher.substring(12,18));
returnstring = Character.toString(A) + Character.toString(B) +
Character.toString(C);
}
if(cipher.length()==24)
{
char A = decipherChar(cipher.substring(0,6));
char B = decipherChar(cipher.substring(6,12));
char C = decipherChar(cipher.substring(12,18));
char D = decipherChar(cipher.substring(18,24));
returnstring = Character.toString(A) + Character.toString(B) +
Character.toString(C) + Character.toString(D);
}
return returnstring;
答案 1 :(得分:-1)
好吧,我会给你带来疑问的好处,并帮助你用一些代码来帮助你入门。并且可能会有一些错误,所以要小心......
public static String decipher(String cipher){
String deciphered = null;
//check if cipher is valid
if(isValid(cipher)){
char[] arrChar = new char[cipher.length()/6];
for(int i = 0; i< cipher.length()/6; i++){
String substring = cipher.substring(i, i+6);
arrChar[i] = decipherChar(substring);
//now you need to combine the array of char into a string.
}
}
else{
return _msg;
}
return deciphered;
}