题解 | #字符串加解密#

字符串加解密

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


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String strEn = sc.next();
            String strDe = sc.next();
            System.out.println(enCode(strEn));
            System.out.println(deCode(strDe));
        }
    }

    private static String deCode(String strDe) {
        char[] chars = strDe.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (Character.isLetter(chars[i])) {
                if (chars[i] == 'a') {
                    chars[i] = 'z';
                } else if (chars[i] == 'A') {
                    chars[i] = 'Z';
                } else {
                    chars[i] -= 1;
                }
                if (Character.isUpperCase(chars[i])) {
                    chars[i] = Character.toLowerCase(chars[i]);
                } else {
                    chars[i] = Character.toUpperCase(chars[i]);
                }
            } else if (Character.isDigit(chars[i])) {
                if (chars[i] == '0') {
                    chars[i] = '9';
                } else {
                    chars[i] -= 1;
                }
            }
        }
        return String.valueOf(chars);
    }

    private static String enCode(String strEn) {
        char[] chars = strEn.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (Character.isLetter(chars[i])) {
                if (chars[i] == 'z') {
                    chars[i] = 'a';
                } else if (chars[i] == 'Z') {
                    chars[i] = 'A';
                } else {
                    chars[i] += 1;
                }
                if (Character.isLowerCase(chars[i])) {
                    chars[i] = Character.toUpperCase(chars[i]);
                } else {
                    chars[i] = Character.toLowerCase(chars[i]);
                }
            } else if (Character.isDigit(chars[i])) {
                if (chars[i] == '9') {
                    chars[i] = '0';
                } else {
                    chars[i] += 1;
                }
            }
        }
        return String.valueOf(chars);
    }

}
全部评论

相关推荐

比亚迪汽车新技术研究院 硬件工程师 总包21左右 硕士
点赞 评论 收藏
分享
头像
11-21 11:39
四川大学 Java
是红鸢啊:忘了还没结束,还有字节的5k 违约金
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务