题解 | #字符串加解密#

字符串加解密

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

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let  num = 0
rl.on('line', function (line) {
    num++
    if(num === 1){
        // 对字符串进行加密、let与const与花括号
        let strArr = line.split('')
        const res = []
        let char = '' 
        for(let i = 0; i < line.length; i++){
            // 是字母
            if(/[a-zA-Z]/.test(line[i])){
                let code = line[i].charCodeAt()
                if(code === 90){
                    code = 65
                    char = String.fromCharCode(code)
                }else if(code === 122){
                    code = 97
                    char = String.fromCharCode(code)
                }else {
                    char = String.fromCharCode(code + 1)
                }
                // 往后转一位,然后变大写
                if(code >= 65 && code <= 90){
                    char = char.toLowerCase()
                }else if(code >= 97 && code<= 122){
                    // 往后转一位,然后变小写
                    char = char.toLocaleUpperCase()
                }
            }else if(/[0-9]/.test(line[i])){
                // 数字往后加1
                if(line[i] === '9'){
                    char = '0'
                }else {
                    char = line[i]*1 + 1 + ''
                }
            }
            res.push(char)
        }
        console.log(res.join(''))
    }else if(num ===  2){
        // 对字符串进行解密、let与const与花括号
        let strArr = line.split('')
        const res = []
        let char = '' 
        for(let i = 0; i < line.length; i++){
            // 是字母
            if(/[a-zA-Z]/.test(line[i])){
                let code = line[i].charCodeAt()
                if(code === 65){
                    code = 90
                    char = String.fromCharCode(code)
                }else if(code === 97){
                    code = 122
                    char = String.fromCharCode(code)
                }else {
                    char = String.fromCharCode(code - 1)
                }
                // 往后转一位,然后变小写
                if(code >= 65 && code <= 90){
                    char = char.toLowerCase()
                }else if(code >= 97 && code<= 122){
                    // 往后转一位,然后变小写
                    char = char.toLocaleUpperCase()
                }
            }else if(/[0-9]/.test(line[i])){
                // 数字往后加1
                if(line[i] === '0'){
                    char = '9'
                }else {
                    char = line[i]*1 - 1 + ''
                }
            }
            res.push(char)
        }
        console.log(res.join(''))
    }
});

全部评论

相关推荐

昨天 10:56
门头沟学院 Java
点赞 评论 收藏
分享
见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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