用javasript 实现在这个型中您将得到一个字符串您的任务将返回一个int列表详细说明大写字母、小写字母、数字和特殊字符的计数如下所示。
以下是使用JavaScript实现该功能的代码:
function countChars(str) {
  let upperCount = 0;
  let lowerCount = 0;
  let digitCount = 0;
  let specialCount = 0;
  for (let i = 0; i < str.length; i++) {
    let c = str.charAt(i);
    if (c >= 'A' && c <= 'Z') {
      upperCount++;
    } else if (c >= 'a' && c <= 'z') {
      lowerCount++;
    } else if (c >= '0' && c <= '9') {
      digitCount++;
    } else {
      specialCount++;
    }
  }
  return [upperCount, lowerCount, digitCount, specialCount];
}
// Example Usage
let str = "#HelloWorld123!";
let counts = countChars(str);
console.log(counts); // [2, 8, 3, 2]
此代码将遍历字符串中的每个字符,并根据其属于哪个类别,增加相应的计数器。最后,将计数器的值存储在一个数组中,并将其返回。在示例用法中,字符串“#HelloWorld123!”的计数器将返回[2,8,3,2],表示它包含2个大写字母,8个小写字母,3个数字和2个特殊字符。
原文地址: https://www.cveoy.top/t/topic/8cz 著作权归作者所有。请勿转载和采集!