JAVA问题~注意是 JAVA算法问题!谢谢各位帮忙解答!!!!!

来源:百度知道 编辑:UC知道 时间:2024/09/19 16:08:39
一个字符串可用事先给定的字母映射表进行加密。例如,设字母映射表为:
abcdefghijklmnopqrstuvwxyz
ngzqtcobmuhelkpdawxfyivrsj
则字符串“abc”被加密为“ngz”。
使用适当的数据结构(不得直接使用数组), 写一静态方法将输入的字符串进行加密后输出;
public static String encrypt(String str) ;
另写一静态方法,将输入的已加密文本串进行解密后输出.
public static String decrypt(String str) ;

简单的字符串处理
经验证可用

private static String args1 = "abcdefghijklmnopqrstuvwxyz";
private static String args2 = "ngzqtcobmuhelkpdawxfyivrsj";

public static String encrypt(String str){
if(null==str||str.equals("")){
return null;
}
String newStr=str;
while(str.length()>0){
String oldChar = str.substring(0,1);
int newCharIndex = args1.indexOf(oldChar);
String newChar = args2.substring(newCharIndex,newCharIndex+1);
newStr = newStr.replaceAll(oldChar, newChar);
str = str.replaceAll(oldChar, "");
}
return newStr;
}

public static String decrypt(String str) {
if(null==str||str.equals("")){
return null;
}
String newStr=str;
while(str.length()>0){
String oldChar = str.substring(0,1);
int newCharIndex = args2.indexOf(oldChar);
String newChar = args1.substring(newCharIndex,new