题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String c = in.nextLine();
String en = in.nextLine();
System.out.println(coding(c));
System.out.println(enCoding(en));
}
static String coding(String s){
StringBuilder sb = new StringBuilder();
for(int i=0; i<s.length(); i++){
char ch = s.charAt(i);
if(Character.isDigit(ch)){
sb.append((ch-'0'+1)%10);
}else{
if(Character.isUpperCase(ch)){
int ind = (ch-'A'+1)%26;
sb.append((char)('a'+ind));
}else{
int ind = (ch-'a'+1)%26;
sb.append((char)('A'+ind));
}
}
}
return sb.toString();
}
static String enCoding(String s){
StringBuilder sb = new StringBuilder();
for(int i=0; i<s.length(); i++){
char ch = s.charAt(i);
if(Character.isDigit(ch)){
sb.append((ch-'0'+9)%10);
}else{
if(Character.isUpperCase(ch)){
int ind = (ch-'A'+25)%26;
sb.append((char)('a'+ind));
}else{
int ind = (ch-'a'+25)%26;
sb.append((char)('A'+ind));
}
}
}
return sb.toString();
}
}
查看4道真题和解析
