题解 | #字符串加解密#
字符串加解密
http://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
let line
function getRes(str,order){
let arr = str.split('');
let temp = ''
for(let i =0;i<arr.length;i++){
let reg = /[a-z]/
let reg1 = /[A-Z]/
let reg2 = /[0-9]/
if(order){
if(reg.test(arr[i])){
let dot = arr[i].toUpperCase().charCodeAt(0);
if(dot == 'Z'.charCodeAt(0)){
temp+='A'
}else{
let res = String.fromCharCode(dot + 1)
temp+=res
}
}else if(reg1.test(arr[i])){
let dot = arr[i].toLowerCase().charCodeAt(0);
if(dot == 'z'.charCodeAt(0)){
temp+='a'
}else{
let res = String.fromCharCode(dot + 1)
temp+=res
}
} else if(reg2.test(arr[i])){
if(arr[i] == '9'){
temp+='0'
}else{
let res = parseInt(arr[i]) + 1;
temp+=res.toString()
}
}else{
temp+= arr[i]
}
}else{
if(reg.test(arr[i])){
let dot = arr[i].toUpperCase().charCodeAt(0);
if(dot == 'A'.charCodeAt(0)){
temp+='Z'
}else{
let res = String.fromCharCode(dot - 1)
temp+=res
}
}else if(reg1.test(arr[i])){
let dot = arr[i].toLowerCase().charCodeAt(0);
if(dot == 'a'.charCodeAt(0)){
temp+='z'
}else{
let res = String.fromCharCode(dot - 1)
temp+=res
}
}else if(reg2.test(arr[i])){
if(arr[i] == '0'){
temp+='9'
}else{
let res = parseInt(arr[i]) - 1;
temp+=res.toString()
}
}else{
temp+= arr[i]
}
}
}
console.log(temp)
}
let i = 0;
while(line = readline()){
getRes(line,i % 2 == 0)
i++
}