프로그래머스 대소문자 바꿔서 출력하기 Lv.0

KUKJIN LEE's profile picture

KUKJIN LEE2개월 전 작성

대소문자 변환 함수를 사용할 수 있냐, 문자 범위를 비교 할 수 있냐를 물어보는 간단한 문제입니다.

 

입력받은 문자열을 루프를 통해 하나씩 탐색했습니다.

 

이후 문자열을 result에 추가하여 반환했습니다.

 

우선 문자 범위 비교입니다. 입력받은 문자열을 하나씩 검토합니다. 'A'보다 크고, 'Z'보다 작다면 대문자이기 때문에, toLowerCase();를 사용했고 조건에 맞지 않는다면 전부 toUpperCase()를 사용해 대문자로 변경했습니다.

 

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

let input = [];

rl.on('line', function (line) {
    input = [line];
    rl.close();
}).on('close',function(){
    str = input[0];
    
    let result = '';
    for(let i = 0; i < str.length; i++) {
        if(str[i] >= 'A' && str[i] <= 'Z'){
            result += str[i].toLowerCase();
        } else {
            result += str[i].toUpperCase();
        }
    }
    console.log(result);
});

New Tech Posts