프로그래머스/코딩 기초 트레이닝
[프로그래머스] 코딩테스트 연습 > 코딩 기초 트레이닝 > 대소문자 바꿔서 출력하기
Jayksss
2024. 11. 15. 00:55
문제 설명
영어 알파벳으로 이루어진 문자열 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(''))
});