题解 | #字符串加解密#
字符串加解密
http://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
import java.util.; import java.io.; import java.util.stream.*;
public class Main{
public static void main(String[] args){
InputStreamReader reader = new InputStreamReader(System.in);
try(BufferedReader br = new BufferedReader(reader)){
String origin = br.readLine();
String encrypt = br.readLine();
char[] origin_chars = origin.toCharArray();
ArrayList<Character> origin_list = new ArrayList<>();
for (int i = 0; i < origin_chars.length; i++) {
origin_list.add(origin_chars[i]);
}
char[] encrypt_chars = encrypt.toCharArray();
ArrayList<Character> encrypt_list = new ArrayList<>();
for (int i = 0; i < encrypt_chars.length; i++) {
encrypt_list.add(encrypt_chars[i]);
}
if(origin.trim().length() ==0 ||encrypt.trim().length() ==0 ) return;
List<Integer> encrypt_result = Encrypt(origin_list);
encrypt_result.forEach((i)-> System.out.print((char)((int)i)));
System.out.println();
List<Integer> decrypt_result = Decrypt(encrypt_list);
decrypt_result.forEach((i)->System.out.print((char)((int)i)));
}catch (IOException e){
System.out.println(e.getMessage());
}
}
public static List<Integer> Encrypt(ArrayList<Character> input){
List<Integer> collect = input.stream().map((i) -> {
if (Character.isLetter(i)) {
//大写
if (i <= 90) {
i = Character.toLowerCase(i);
} else {
i = Character.toUpperCase(i);
}
//Z -> a
if (i == 122) return 97;
//z -> A
if (i==90) return 65;
return i+1;
} else {
if (i < '9') return i + 1;
else return i - 9;
}
}).collect(Collectors.toList());
return collect;
}
public static List<Integer> Decrypt(ArrayList<Character> input){
List<Integer> collect = input.stream().map((i) -> {
if (Character.isLetter(i)) {
//a->Z
if (i == 97) return 90;
//A -> z
if (i==65) return 122;
i= (char)(i-1);
//小写
if (i>= 97 ) {
i = Character.toUpperCase(i);
} else {
i = Character.toLowerCase(i);
}
return i+0;
} else {
if (i == '0' ) return i + 9;
else return i-1;
}
}).collect(Collectors.toList());
return collect;
}
}