题解 | #字符串加解密#

https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a

import java.util.*;

public class Main {

    static String plainDict = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    static String encryptDict = "BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza1234567890";
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String plainText = scanner.nextLine();
        System.out.println(encrypt(plainText));
        String encrypted = scanner.nextLine();
        System.out.println(decrypt(encrypted));
    }
    private static String encrypt(String plainText){
        StringBuilder sb = new StringBuilder();
        for (char ch : plainText.toCharArray()) {
            sb.append(encryptDict.charAt(plainDict.indexOf(ch)));
        }
        return sb.toString();
    }
    private static String decrypt(String encrypted){
        StringBuilder sb = new StringBuilder();
        for (char ch : encrypted.toCharArray()) {
            sb.append(plainDict.charAt(encryptDict.indexOf(ch)));
        }
        return sb.toString();
    }
}

全部评论

相关推荐

仁者伍敌:难怪小公司那么挑剔,让你们这些大佬把位置拿了
点赞 评论 收藏
分享
墨西哥大灰狼:如果你的校友卤馆还在的话,他肯定会给你建议的,可是卤馆注销了@ 程序员卤馆
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务