/*
描述
題目描述
1、對輸入的字符串進行加解密,並輸出。
2加密方法爲:
當內容是英文字母時則用該英文字母的後一個字母替換,同時字母變換大小寫,如字母a時則替換爲B;字母Z時則替換爲a;
當內容是數字時則把該數字加1,如0替換1,1替換2,9替換0;
其他字符不做變化。
3、解密方法爲加密的逆過程。
接口描述:
實現接口,每個接口實現1個基本操作:
void Encrypt (char aucPassword[], char aucResult[]):在該函數中實現字符串加密並輸出
說明:
1、字符串以\0結尾。
2、字符串最長100個字符。
int unEncrypt (char result[], char password[]):在該函數中實現字符串解密並輸出
說明:
1、字符串以\0結尾。
2、字符串最長100個字符。
知識點 字符串
運行時間限制 10M
內存限制 128
輸入
輸入說明
輸入一串要加密的密碼
輸入一串加過密的密碼
輸出
輸出說明
輸出加密後的字符
輸出解密後的字符
樣例輸入 abcdefg BCDEFGH
樣例輸出 BCDEFGH abcdefg
*/
import java.util.Scanner;
public class Main {
public static void main(String args[])
{
Scanner sca = new Scanner(System.in);
String aucPassword = sca.next();
String password = sca.next();
sca.close();
String aucResult = null, result = null;
aucResult = Encrypt(aucPassword);
result = unEncrypt(password);
System.out.println(aucResult);
System.out.println(result);
}
//加密aucPassword爲aucResult
static String Encrypt (String aucPassword)
{
StringBuffer sb = new StringBuffer();
for(int i = 0;i < aucPassword.length(); i++)
{
char c = aucPassword.charAt(i);
if((c >= '0') && (c <= '9'))
{
if(c != '9')
sb.append((char)(c + 1));
else
sb.append((char)(c - ('9' - '0')));
}
else
{
if(Character.isUpperCase(c))
{
if(c != 'Z')
sb.append((char)(c+('b'-'A')));
else
sb.append((char)(c+('a'-'Z')));
}
else
{
if(c != 'z')
sb.append((char)(c - ('a'-'B')));
else
sb.append((char)(c - ('z'-'A')));
}
}
}
return sb.toString();
}
//解密password爲result
static String unEncrypt (String password)
{
StringBuffer sb = new StringBuffer();
for(int i = 0;i < password.length(); i++)
{
char c = password.charAt(i);
if((c >= '0') && (c <= '9'))
{
if(c != '0')
sb.append((char)(c - 1));
else
sb.append((char)(c + ('9' - '0')));
}
else
{
if(Character.isUpperCase(c))
{
if(c != 'A')
sb.append((char)(c + ('a'-'B')));
else
sb.append((char)(c + ('z'-'A')));
}
else
{
if(c != 'a')
sb.append((char)(c - ('b'-'A')));
else
sb.append((char)(c - ('a'-'Z')));
}
}
}
return sb.toString();
}
}
自己想的算法,寫的代碼,希望得到更多的建議提高自己!
字符串加解密
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.