Image

Lv.0 🔗문자열 돌리기

📝문제 요약

문제 설명

문자열 str이 주어집니다.

문자열을 시계방향으로 90도 돌려서 아래 입출력 예와 같이 출력하는 코드를 작성해 보세요.


제한사항

1 ≤ str의 길이 ≤ 10


입출력 예

입력 #1

abcde

출력 #1

a b c d e


✏️문제 풀이

  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 () {
      // 나중에 로직 실행
    });
    
    • readline 모듈을 사용해 입력을 받음
    • 한 줄의 문자열을 입력받아 input[0]에 저장
    • 입력이 한 줄이므로 input = [line];로 처리하고 바로 close() 호출
  2. 출력 로직

    str = input[0]; // 입력된 문자열
    
    for (let i = 0; i < str.length; i++) {
      // 문자열을 순회
      console.log(str[i]); // 해당 위치의 문자열 출력
    }
    
    • 문자열의 각 문자를 순회하면서
      • 해당 위치의 문자열을 출력


💯제출 코드

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];

  for (let i = 0; i < str.length; i++) {
    console.log(str[i]);
  }
});

댓글남기기