1. 문자열 출력하기
[문제 설명]
문자열 str이 주어질 때, str을 출력하는 코드를 작성해 보세요.
[제한사항]
- 1 ≤ str의 길이 ≤ 1,000,000
- 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];
console.log(str);
});
[답변 2]
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
}).on('line', console.log)
2. a와 b 출력하기
[문제 설명]
정수 a와 b가 주어집니다. 각 수를 입력받아 입출력 예와 같은 형식으로 출력하는 코드를 작성해 보세요.
[제한사항]
- -100,000 ≤ a, b ≤ 100,000
[답변]
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let input = [];
rl.on('line', function (line) {
input = line.split(' ');
}).on('close', function () {
console.log("a = " + Number(input[0]));
console.log("b = " + Number(input[1]))
});
[답변2]
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
}).on('line', function (line) {
const [a, b] = line.split(' ')
console.log('a =', a)
console.log('b =', b)
})
3. 문자열 반복해서 출력하기
[문제 설명]
문자열 str과 정수 n이 주어집니다.
str이 n번 반복된 문자열을 만들어 출력하는 코드를 작성해 보세요.
[제한사항]
- 1 ≤ str의 길이 ≤ 10
- 1 ≤ n ≤ 5
[답변]
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let input = [];
rl.on('line', function (line) {
input = line.split(' ');
}).on('close', function () {
str = input[0];
n = Number(input[1]);
console.log(str.repeat(n));
});
📌 repeat() : 주어진 문자열을 count만큼 반복하여 붙여 새로운 문자열로 반환
4. 대소문자 바꿔서 출력하기
[문제 설명]
영어 알파벳으로 이루어진 문자열 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(){
let arr = input[0].split("");
for(let i=0; i<arr.length; i++){
if(arr[i]===arr[i].toUpperCase()){
arr[i]=arr[i].toLowerCase();
}else{
arr[i]=arr[i].toUpperCase();
}
}
console.log(arr.join(''));
});
📌 대소문자 변환
- toUpperCase() : 대문자로 변환
- toLowerCase() : 소문자로 변환
📌 배열을 문자열로 출력 = join()
- join() : a,b,c
- join('-') : a-b-c
- join('') : abc
5. 특수문자 출력하기
[문제 설명]
다음과 같이 출력하도록 코드를 작성해주세요.
[출력 예시]
!@#$%^&*(\'"<>?:;
[답변]
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('close', function () {
console.log(`!@#$%^&*(\\'"<>?:;`)
});
📌 \ 역슬래시를 이용한 특수문자 처리
728x90
반응형
'프로그래머스 > Lv. 0 코딩 기초 트레이닝' 카테고리의 다른 글
[프로그래머스] 코딩 기초 트레이닝 Day 6 / JS (0) | 2023.07.25 |
---|---|
[프로그래머스] 코딩 기초 트레이닝 Day 5 / JS (0) | 2023.07.24 |
[프로그래머스] 코딩 기초 트레이닝 Day 4 / JS (0) | 2023.07.23 |
[프로그래머스] 코딩 기초 트레이닝 Day 3 / JS (0) | 2023.07.22 |
[프로그래머스] 코딩 기초 트레이닝 Day 2 / JS (0) | 2023.07.21 |