题解 | #字符串加解密#
字符串加解密
http://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String strEn = sc.next();
String strDe = sc.next();
System.out.println(enCode(strEn));
System.out.println(deCode(strDe));
}
}
private static String deCode(String strDe) {
char[] chars = strDe.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (Character.isLetter(chars[i])) {
if (chars[i] == 'a') {
chars[i] = 'z';
} else if (chars[i] == 'A') {
chars[i] = 'Z';
} else {
chars[i] -= 1;
}
if (Character.isUpperCase(chars[i])) {
chars[i] = Character.toLowerCase(chars[i]);
} else {
chars[i] = Character.toUpperCase(chars[i]);
}
} else if (Character.isDigit(chars[i])) {
if (chars[i] == '0') {
chars[i] = '9';
} else {
chars[i] -= 1;
}
}
}
return String.valueOf(chars);
}
private static String enCode(String strEn) {
char[] chars = strEn.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (Character.isLetter(chars[i])) {
if (chars[i] == 'z') {
chars[i] = 'a';
} else if (chars[i] == 'Z') {
chars[i] = 'A';
} else {
chars[i] += 1;
}
if (Character.isLowerCase(chars[i])) {
chars[i] = Character.toUpperCase(chars[i]);
} else {
chars[i] = Character.toLowerCase(chars[i]);
}
} else if (Character.isDigit(chars[i])) {
if (chars[i] == '9') {
chars[i] = '0';
} else {
chars[i] += 1;
}
}
}
return String.valueOf(chars);
}
}