[203] 10820번 문자열 분석

younoah·2021년 11월 13일
0

[백준-기초]

목록 보기
15/29

10820번 문자열 분석

문제

문자열 N개가 주어진다. 이때, 문자열에 포함되어 있는 소문자, 대문자, 숫자, 공백의 개수를 구하는 프로그램을 작성하시오.

각 문자열은 알파벳 소문자, 대문자, 숫자, 공백으로만 이루어져 있다.

입력

첫째 줄부터 N번째 줄까지 문자열이 주어진다. (1 ≤ N ≤ 100) 문자열의 길이는 100을 넘지 않는다.

출력

첫째 줄부터 N번째 줄까지 각각의 문자열에 대해서 소문자, 대문자, 숫자, 공백의 개수를 공백으로 구분해 출력한다.

예제 입력 1 복사

This is String
SPACE    1    SPACE
 S a M p L e I n P u T     
0L1A2S3T4L5I6N7E8

예제 출력 1 복사

10 2 0 2
0 10 1 8
5 6 0 16
0 8 9 0

코드

//---- 세팅 ----//
const fs = require('fs');
const stdin = (
  process.platform === 'linux'
    ? fs.readFileSync('/dev/stdin').toString()
    : `\
This is String
SPACE    1    SPACE
  S a M p L e I n P u T
0L1A2S3T4L5I6N7E8
`
).split('\n');

const input = (() => {
  let line = 0;
  return () => stdin[line++];
})();

//---- 풀이 -----//
const strArr = [];

while (true) {
  const str = input();
  if (str === '') break;
  strArr.push(str);
}

const calcStr = str => {
  let s = 0;
  let b = 0;
  let n = 0;
  let space = 0;

  str.split('').forEach(char => {
    const code = char.charCodeAt();
    if (65 <= code && code <= 90) b++;
    if (97 <= code && code <= 122) s++;
    if (48 <= code && code <= 57) n++;
    if (char === ' ') space++;
  });

  return [s, b, n, space];
};

strArr.forEach(str => {
  console.log(calcStr(str).join(' '));
});

풀이

단순하게 입력받은 문자열을 순회하면서 대문자, 소문자, 공백, 숫자 일 떄 카운트업을 하여 출력한다.

profile
console.log(noah(🍕 , 🍺)); // true

0개의 댓글