题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { // Write your code here let arr = []; rl.on("line", function (line) { arr.push(line); }).on("close", function () { console.log(encryption(arr[0])); console.log(decryption(arr[1])); function encryption(str) { let list = str.split(""); for (let i = 0; i < list.length; i++) { if (list[i].match(/[A-Z]/)) { list[i] = list[i] == "Z" ? "a" : String.fromCharCode( list[i].toLowerCase().charCodeAt(0) + 1 ); } else if (list[i].match(/[a-z]/)) { list[i] = list[i] == "z" ? "A" : String.fromCharCode( list[i].toUpperCase().charCodeAt(0) + 1 ); } else if (list[i].match(/[0-9]/)) { list[i] = list[i] == "9" ? "0" : (parseInt(list[i]) + 1).toString(); } } return list.join('') } function decryption(str) { let list = str.split(""); for (let i = 0; i < list.length; i++) { if (list[i].match(/[A-Z]/)) { list[i] = list[i] == "A" ? "z" : String.fromCharCode( list[i].toLowerCase().charCodeAt(0) - 1 ); } else if (list[i].match(/[a-z]/)) { list[i] = list[i] == "a" ? "Z" : String.fromCharCode( list[i].toUpperCase().charCodeAt(0) - 1 ); } else if (list[i].match(/[0-9]/)) { list[i] = list[i] == "0" ? "9" : (parseInt(list[i]) - 1).toString(); } } return list.join('') } }); })();