ABOUT ME

Today
Yesterday
Total
  • 10808. 알파벳 개수
    Baekjoon 2023. 5. 7. 17:57

     문제 

    알파벳 소문자로만 이루어진 단어 S가 주어진다. 각 알파벳이 단어에 몇 개가 포함되어 있는지 구하는 프로그램을 작성하시오.

     

     입력 

    첫째 줄에 단어 S가 주어진다. 단어의 길이는 100을 넘지 않으며, 알파벳 소문자로만 이루어져 있다.

    baekjoon
    

     

     출력 

    단어에 포함되어 있는 a의 개수, b의 개수, …, z의 개수를 공백으로 구분해서 출력한다.

    1 1 0 0 1 0 0 0 0 1 1 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0
     
     
     에러 
    더보기
    let fs = require("fs");
    const readFileSyncAdd = "/dev/stdin";
    const input = fs.readFileSync(readFileSyncAdd).toString().trim().split('');
    
    const res = [];
    
    for(let i = 97; i<=122; i++){
        res[i-97] = 0;
        for(v in input){
            input[v] === String.fromCharCode(i) ? res[i-97] += 1 : res[i-97] = res[i-97];
        }
    }
    
    console.log(res);

    사소한 부분에서 틀렸다.

    출력할 때 res 를 그대로 출력하면 어떻게 될까? 바로 배열이 출력된다.

    배열이 아니라 문자열로 출력해야 하므로... join(' ') 추가하면 끝.

    최종 코드는 아래와 같다.

     

     코드 

    let fs = require("fs");
    const readFileSyncAdd = "./10808/10808.txt";
    const input = fs.readFileSync(readFileSyncAdd).toString().trim().split('');
    
    //출력될 부분을 저장하는 배열
    const res = [];
    
    //소문자 a부터 z까지 반복
    for(let i = 97; i<=122; i++){
        res[i-97] = 0;
        for(v in input){
            input[v] === String.fromCharCode(i) ? res[i-97] += 1 : res[i-97] = res[i-97];
        }
    }
    
    console.log(res.join(' '));

    'Baekjoon' 카테고리의 다른 글

    2751. 수 정렬하기 2  (0) 2023.05.07
    1935. 후위 표기식2  (0) 2023.05.07
    11655. ROT13  (0) 2023.05.02
    10845. 큐  (0) 2023.05.02
    10820. 문자열 분석  (0) 2023.05.02
Designed by Tistory.