저장을 습관화

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

코딩 테스트/프로그래머스 - 자바스크립트

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

ctrs 2023. 9. 7. 17:48

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

https://school.programmers.co.kr/learn/courses/30/lessons/181949

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

1. 문제 명

대소문자 바꿔서 출력하기


2. 문제 설명

영어 알파벳으로 이루어진 문자열 str이 주어집니다. 각 알파벳을 대문자는 소문자로 소문자는 대문자로 변환해서 출력하는 코드를 작성해 보세요.


3. 제한 사항

1 ≤ str의 길이 ≤ 20

str은 알파벳으로 이루어진 문자열입니다.


4. 예시

입력 #1

aBcDeFg

 

출력 #1

AbCdEfG

 

5. 기본 제공 코드

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

let input = [];

rl.on('line', function (line) {
    input = [line];
}).on('close',function(){
    str = input[0];
});


6. 제출한 내 답

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

let input = [];

rl.on("line", function (line) {
  input = [line];
}).on("close", function () {
  str = input[0];
  let answer = "";
  for (i = 0; i <= str.length; i++) {
    if (str.charAt(i) === str.charAt(i).toUpperCase()) {
      // 대문자라면 소문자로 변경, answer와 더함
      answer += str.charAt(i).toLowerCase();
    } else if (str.charAt(i) === str.charAt(i).toLowerCase()) {
      // 소문자라면 대문자로 변경, answer와 더함
      answer += str.charAt(i).toUpperCase();
    }
  }
  console.log(answer);
});

 

6-2. VSC에 작성한 내용

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

let input = [];

rl.on("line", function (line) {
  input = [line];
}).on("close", function () {
  str = input[0];
  let answer = "";
  for (i = 0; i <= str.length; i++) {
    if (str.charAt(i) === str.charAt(i).toUpperCase()) {
      // 대문자라면 소문자로 변경, answer와 더함
      answer += str.charAt(i).toLowerCase();
    } else if (str.charAt(i) === str.charAt(i).toLowerCase()) {
      // 소문자라면 대문자로 변경, answer와 더함
      answer += str.charAt(i).toUpperCase();
    }
  }
  console.log(answer);
});

 

테스트

let str0 = "aBcDeFg";
for (i = 0; i < str0.length; i++) {
  // console.log(str0[i]);

  // 대문자인지 확인, 맞다면 소문자로 변경
  if (str0[i] === str0[i].toUpperCase()) {
    console.log(str0.charAt(i));
    console.log("대문자입니다.");
    console.log(`소문자로 바꾸면 ${str0.charAt(i).toLowerCase()}가 됩니다.`);
  } // 소문자인지 확인, 맞다면 대문자로 변경
  else if (str0.charAt(i) === str0.charAt(i).toLowerCase()) {
    console.log(str0.charAt(i));
    console.log("소문자입니다.");
    console.log(`대문자로 바꾸면 ${str0.charAt(i).toUpperCase()}가 됩니다.`);
  }
}

let str = "aBcDeFg";
let answer = "";

for (i = 0; i <= str.length; i++) {
  if (str.charAt(i) === str.charAt(i).toUpperCase()) {
    // 대문자라면 소문자로 변경, answer와 더함
    // console.log(answer);
    answer += str.charAt(i).toLowerCase();
  } else if (str.charAt(i) === str.charAt(i).toLowerCase()) {
    // 소문자라면 대문자로 변경, answer와 더함
    // console.log(answer);
    answer += str.charAt(i).toUpperCase();
  }
}
console.log(answer);

 

7. 특이사항

문제를 자세히 보기

문제에 출력하라고 써있으면 출력까지 명확하게 하기


8. 다른 사람이 작성한 답

8-1.

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

let input = [];

rl.on('line', function (line) {
    input = [line];
}).on('close',function(){
    str = input[0].split('');
    str.forEach((value, index) => {
        if (value === value.toUpperCase()) {
            str[index] = value.toLowerCase();
        } else {
            str[index] = value.toUpperCase();
        }
    });
    console.log(str.join(''));
});

str을 .spilit('')을 사용해 쪼갬, 배열의 형태로 변경

forEach를 사용하여 하나씩 검사, 대소문자 확인 후 변경

 

8-2. 

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

let input;

rl.on('line', (line) => {
  input = [...line];
}).on('close', () => {
  console.log(
    input.map((char) => (/[a-z]/.test(char) ? char.toUpperCase() : char.toLowerCase())).join(''),
  );
});

.test() 메소드 사용..

변수 char이 a-z에 포함되는가, 즉 소문자인가 확인

이후 삼항연산자를 사용

참이라면 대문자로 변경 후 .join

거짓이라면 소문자로 변경 후(이 부분은 없어도 될것 같은데 어차피 소문자이니까) .join