문자열 N개가 주어진다. 이때, 문자열에 포함되어 있는 소문자, 대문자, 숫자, 공백의 개수를 구하는 프로그램을 작성하시오.
각 문자열은 알파벳 소문자, 대문자, 숫자, 공백으로만 이루어져 있다.
첫째 줄부터 N번째 줄까지 문자열이 주어진다. (1 ≤ N ≤ 100) 문자열의 길이는 100을 넘지 않는다.
첫째 줄부터 N번째 줄까지 각각의 문자열에 대해서 소문자, 대문자, 숫자, 공백의 개수를 공백으로 구분해 출력한다.
This is String
SPACE 1 SPACE
S a M p L e I n P u T
0L1A2S3T4L5I6N7E8
10 2 0 2
0 10 1 8
5 6 0 16
0 8 9 0
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on("line", function (line) {
const result = analyzeStr(line);
console.log(result.join(" ")); // result 배열 요소를 공백으로 구분해 문자열로 변환한 후, 콘솔에 출력
});
function analyzeStr(str) {
let lower = 0; //소문자
let upper = 0; //대문자
let number = 0; //숫자
let space = 0; //공백
for (let i = 0; i < str.length; i++) { // for문으로 str의 길이까지 돌리기
const char = str.charAt(i); // str의 i번째 인덱스에 있는 문자를 반환
if (char >= "a" && char <= "z") { //소문자
lower++;
} else if (char >= "A" && char <= "Z") { //대문자
upper++;
} else if (char >= "0" && char <= "9") { //숫자
number++;
} else if (char === " ") { // 공백
space++;
}
}
return [lower, upper, number, space];
}