题解 | #字符串加解密#

字符串加解密

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();
    }
}
全部评论

相关推荐

10-28 14:42
门头沟学院 Java
watermelon1124:因为嵌入式炸了
点赞 评论 收藏
分享
孤寡孤寡的牛牛很热情:为什么我2本9硕投了很多,都是简历或者挂,难道那个恶心人的测评真的得认真做吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务