题解 | #统计字符#

统计字符

https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5

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

rl.on('line', (line) => {
    let eCount = 0, sCount = 0, dCount = 0, oCount = 0;
    for(let i = 0; i < line.length; i++) {
        const char = line[i];
        if(/[A-Za-z]/.test(char)) eCount++;
        else if(/\d/.test(char)) dCount++;
        else if(char === ' ') sCount++;
        else oCount++;
    }

    console.log(eCount);
    console.log(sCount);
    console.log(dCount);
    console.log(oCount);
})

正则匹配:

英文字符:[A-Za-z]

数字:/\d/

空白符: /\s/

全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务