题解 | #字符串加解密#

字符串加解密

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

q29 思路: 暴力直接做 a/z或者A/Z做判断 其余字母按+/-32转换大小写


public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str1 = sc.nextLine();
        String str2 = sc.nextLine();

        for(int i = 0;i < str1.length();i++){
            if(str1.charAt(i) == 'Z')
                System.out.print('a');
            else if(str1.charAt(i) == 'z')
                System.out.print('A');
            else if('0' <= str1.charAt(i) && str1.charAt(i) <= '9'){
                if(str1.charAt(i) < '9')
                    System.out.print((char)(str1.charAt(i) + 1));
                else
                    System.out.print('0');
            }
            else if('A' <= str1.charAt(i) && str1.charAt(i) < 'Z'){
                char c = (char)((int)(str1.charAt(i)) + 32 + 1);
                System.out.print(c);
            }
            else if('a' <= str1.charAt(i) && str1.charAt(i) < 'z'){
                char c = (char)((int)(str1.charAt(i)) - 32 + 1);
                System.out.print(c);
            }
            else
                System.out.print(str1.charAt(i));
        }
        System.out.println();

        for(int i = 0;i < str2.length();i++){
            if(str2.charAt(i) == 'A')
                System.out.print('z');
            else if(str2.charAt(i) == 'a')
                System.out.print('Z');
            else if('0' <= str2.charAt(i) && str2.charAt(i) <= '9'){
                if(str2.charAt(i) > '0')
                    System.out.print((char)(str2.charAt(i) - 1));
                else
                    System.out.print('9');
            }
            else if('A' < str2.charAt(i) && str2.charAt(i) <= 'Z'){
                char c = (char)((int)(str2.charAt(i)) + 32 - 1);
                System.out.print(c);
            }
            else if('a' < str2.charAt(i) && str2.charAt(i) <= 'z'){
                char c = (char)((int)(str2.charAt(i)) - 32 - 1);
                System.out.print(c);
            }
            else
                System.out.print(str2.charAt(i));
        }
    }
}
全部评论

相关推荐

06-23 11:28
门头沟学院 Java
牛客91966197...:也有可能是点拒绝的时候自动弹的话术
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
今天 13:35
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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