面试题字符加密

1.将一串字符串,进行加密。

例如:cbna你哈jbj897

输出:xymz你哈qyq213

加密规则 a<–>z A<–>Z 1<–>9

现在回顾起来,这是一个比较简单的加密。但是我在面试的时候,却写错了。┭┮﹏┭┮,看来还是


public String revsered(String s){
    char[] chars = s.toCharArray();
    for(int i=0;i<chars.length;i++){
        if(chars[i]>='a'&&chars[i]<='z'){
                chars[i] = (char) ('z'-(chars[i]-'a'));
        }
        if(chars[i]>='A'&&chars[i]<='Z'){
            chars[i] = (char) ('Z'-(chars[i]-'A'));
        }
        if(chars[i]>='1'&&chars[i]<='9'){
            chars[i] = (char) ('1'-(chars[i]-'9'));
        }
    }
    return new String(chars);
}
public static void main(String[] args){
  Demo1 d = new Demo1();
  String s = d.revsered("cbna你哈jbj897");//xymz你哈qyq213
  System.out.println(s);

}

猜你喜欢

转载自blog.csdn.net/qq_40981730/article/details/83045950