题解 | #字符串加解密#

字符串加解密

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(''))
    }
});

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 14:35
点赞 评论 收藏
分享
代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
强大的马里奥:不太可能,我校计算机硕士就业率99%
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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