题解 | #字符串加解密#

字符串加解密

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

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) { 
            String s1 = in.next();
            String s2 = in.next();
            char[] c = s1.toCharArray();
            char[] c1 = s2.toCharArray();
            for(int i = 0; i<c.length; i++){
                if(c[i]>='0' && c[i]<='9'){
                    c[i] = (char)((int)c[i] + 1);
                    if(c[i] > '9'){
                        c[i] = '0';
                    }
                }else if(c[i]>='a' && c[i]<='z'){
                    c[i] = (char)((int)c[i] - 32 + 1);
                    if(c[i]>'Z'){
                        c[i] = 'A';
                    }
                }else if(c[i]>='A' && c[i]<='Z'){
                    c[i] = (char)((int)c[i] + 32 + 1);
                    if(c[i]>'z'){
                        c[i] = 'a';
                    }
                }
                
            }
            for(int i = 0; i<c1.length; i++){
                if(c1[i]>='0' && c1[i]<='9'){
                    c1[i] = (char)((int)c1[i] - 1);
                    if(c1[i] < '0'){
                        c1[i] = '9';
                    }
                }else if(c1[i]>='a' && c1[i]<='z'){
                    c1[i] = (char)((int)c1[i] - 32 -1);
                    if(c1[i]<'A'){
                        c1[i] = 'Z';
                    }
                }else if(c1[i]>='A' && c1[i]<='Z'){
                    c1[i] = (char)((int)c1[i] + 32 -1);
                    if(c1[i]<'a'){
                        c1[i] = 'z';
                    }
                }
            }
            System.out.println(s1.copyValueOf(c));
            System.out.println(s2.copyValueOf(c1));
        }
    }
}
全部评论

相关推荐

10-15 09:13
已编辑
天津大学 soc前端设计
点赞 评论 收藏
分享
11-03 14:38
重庆大学 Java
AAA求offer教程:我手都抬起来了又揣裤兜了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务