문제 설명
영어 알파벳으로 이루어진 문자열 str이 주어집니다. 각 알파벳을 대문자는 소문자로 소문자는 대문자로 변환해서 출력하는 코드를 작성해 보세요.
제한사항
1 ≤ str의 길이 ≤ 20
str은 알파벳으로 이루어진 문자열입니다.
내 풀이:
고민하다가 바로 생각나는대로 했더니 통과되더라😃😃
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 result = "";
for(let i in str) {
// 소문자
if(str.charAt(i) != str.charAt(i).toUpperCase()) {
result += str.charAt(i).toUpperCase();
} else {
// 나머지는 대문자
result += str.charAt(i).toLowerCase();
}
}
console.log(result);
});
다른 풀이:
정규식을 사용한 풀이도 좋다고본다.
자주 쓰는 정규식 사용법은 외워둬야지~ 매번 하는데 손이 안간다🤪
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];
const regex = /[A-Z]/;
console.log([...str].map((s)=> regex.test(s) ? s.toLowerCase() : s.toUpperCase()).join(''))
});
'프로그래머스 > 코딩 기초 트레이닝' 카테고리의 다른 글
[프로그래머스] 코딩테스트 연습 > 코딩 기초 트레이닝 > 덧셈식 출력하기 (0) | 2024.11.16 |
---|---|
[프로그래머스] 코딩테스트 연습 > 코딩 기초 트레이닝 > 특수문자 출력하기 (0) | 2024.11.16 |
[프로그래머스] 코딩테스트 연습 > 코딩 기초 트레이닝 > 문자열 정수의 합 (3) | 2024.11.14 |
[프로그래머스] 코딩테스트 연습 > 코딩 기초 트레이닝 > 문자열을 정수로 변환하기 (0) | 2024.11.14 |
[프로그래머스] 코딩테스트 연습 > 코딩 기초 트레이닝 > 문자열로 변환 (0) | 2024.11.14 |