题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
模拟
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); String str1 = br.readLine(), str2 = br.readLine(); pw.println(encrypt(str1)); pw.println(decrypt(str2)); pw.flush(); pw.close(); br.close(); } private static String encrypt(String str) { StringBuilder builder = new StringBuilder(); for (char c : str.toCharArray()) { if (Character.isUpperCase(c)) { c = (char) ((c - 'A' + 1) % 26 + 'a'); } else if (Character.isLowerCase(c)) { c = (char) ((c - 'a' + 1) % 26 + 'A'); } else if (Character.isDigit(c)) { c = (char) ((c - '0' + 1) % 10 + '0'); } builder.append(c); } return builder.toString(); } private static String decrypt(String str) { StringBuilder builder = new StringBuilder(); for (char c : str.toCharArray()) { if (Character.isUpperCase(c)) { c = (char) ((c - 'A' - 1 + 26) % 26 + 'a'); } else if (Character.isLowerCase(c)) { c = (char) ((c - 'a' - 1 + 26) % 26 + 'A'); } else if (Character.isDigit(c)) { c = (char) ((c - '0' - 1 + 10) % 10 + '0'); } builder.append(c); } return builder.toString(); } }