문제
문장이 주어졌을 때, 단어를 모두 뒤집어서 출력하는 프로그램을 작성하시오. 단, 단어의 순서는 바꿀 수 없다. 단어는 영어 알파벳으로만 이루어져 있다.
첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있으며, 문장이 하나 주어진다. 단어의 길이는 최대 20, 문장의 길이는 최대 1000이다. 단어와 단어 사이에는 공백이 하나 있다.
각 테스트 케이스에 대해서, 입력으로 주어진 문장의 단어를 모두 뒤집어 출력한다.
2
I am happy today
We want to win the first prize
I ma yppah yadot
eW tnaw ot niw eht tsrif ezirp
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let inputArr = [];
let testCase = 0;
let count = 0;
rl.on('line', function(line) {
if (testCase === 0) {
// 첫 번째 입력값은 테스트 케이스의 개수
testCase = Number(line);
} else {
// 테스트 케이스의 개수만큼 입력을 받고 처리
inputArr.push(line);
count++;
}
if (count === testCase) {
// 모든 입력을 받았으면 각각의 문장을 뒤집어서 출력
for (let i = 0; i < inputArr.length; i++) {
const words = inputArr[i].split(' ');
const reversedWords = words.map(word => word.split('').reverse().join(''));
console.log(reversedWords.join(' '));
}
rl.close();
}
});
2
I am happy today
We want to win the first prize
I ma yppah yadot
eW tnaw ot niw eht tsrif ezirp