첫 줄에 줄 수 n이 주어진다. 이어서 n줄의 문자열이 주어진다. 줄마다 앞과 뒤의 공백을 모두 지우고, 결과를 대괄호로 감싸 "[결과]"처럼 한 줄에 출력하라. 공백은 빈칸, 탭, 그리고 줄 끝의 \r이다(입력 파일은 Windows식 줄 끝 \r\n일 수도 있다). 글자 사이의 공백은 그대로 둔다. 공백만 있거나 빈 줄이면 "[]"를 출력한다.
입력
1 ≤ n ≤ 100. 각 줄은 빈칸·탭과 ASCII 기호·영어·숫자로 이루어지고 줄 끝 문자를 빼고 길이 0 ~ 300
출력
n줄, 줄마다 [다듬은 문자열]
예시 1 · stdin
3
hello
world
keep inner
stdout
[hello]
[world]
[keep inner]
main.py
오류 목록00F8 ↓ · ⇧F8 ↑
입력(stdin)
실행하면 이 줄들이 차례로 input()·표준 입력으로 들어가요(실행 중에 묻지 않아요).
$ 준비됐어요
코드를 입력해 풀려면 PC에서 열어 주세요. 모바일에서는 문제와 풀이를 볼 수 있어요.
다른 언어로는?언어별 풀이 보기 · 8개 언어 풀이
앞뒤 공백 지우기: Python s.strip(), JS·TS s.trim(), Java s.trim()(코드 32 이하 모두)·s.strip()(유니코드 공백), C# s.Trim(), 크레이콘 s.공백제거(). C와 C++에는 trim이 없어서 C는 isspace로 양 끝을 좁히고, C++는 find_first_not_of(" \t\r\n")/find_last_not_of로 자리를 찾아 substr해요. Windows 줄 끝 함정: 한 줄 읽기가 \r을 떼 주는지가 언어마다 달라요. Java readLine·C# ReadLine은 \r\n을 통째로 떼고(크레이콘 콘솔도 \r을 지워 줘요), Python input()·C fgets·C++ getline·JS split("\n")은 \r을 남겨요. 그래서 C++에서 지울 글자 목록에 \r을 빠뜨리면 줄 끝에 \r이 남고, 그 앞의 빈칸·탭도 지워지지 않아요("hi\t\r" → "[hi\t\r]"). 다른 함정: Python strip(" ")처럼 빈칸만 지정하면 탭과 \r이 남고, " ".join(s.split())은 안쪽 공백까지 하나로 줄여 버려요.
Pythonmain.py · 3 줄
n = int(input())for _ in range(n): print("[" + input().strip() + "]")
JavaScriptmain.js · 4 줄
const [n, ...lines] = require("fs").readFileSync(0, "utf8").split("\n");const out = [];for (let i = 0; i < Number(n); i++) out.push("[" + lines[i].trim() + "]");console.log(out.join("\n"));
TypeScriptmain.ts · 5 줄
import { readFileSync } from "node:fs";const [n, ...lines]: string[] = readFileSync(0, "utf8").split("\n");const out: string[] = [];for (let i = 0; i < Number(n); i++) out.push("[" + lines[i].trim() + "]");console.log(out.join("\n"));
JavaMain.java · 14 줄
import java.io.BufferedReader;import java.io.InputStreamReader;public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); StringBuilder out = new StringBuilder(); for (int i = 0; i < n; i++) { out.append('[').append(br.readLine().strip()).append("]\n"); } System.out.print(out); }}
C#Main.cs · 16 줄
using System;using System.Text;class Program{ static void Main() { int n = int.Parse(Console.ReadLine()); var out_ = new StringBuilder(); for (int i = 0; i < n; i++) { out_.Append('[').Append(Console.ReadLine().Trim()).Append("]\n"); } Console.Write(out_); }}
C++main.cpp · 20 줄
#include <iostream>#include <string>std::string trim(const std::string& s) { const char* ws = " \t\r\n"; size_t a = s.find_first_not_of(ws); if (a == std::string::npos) return ""; size_t b = s.find_last_not_of(ws); return s.substr(a, b - a + 1);}int main() { std::string line; std::getline(std::cin, line); int n = std::stoi(line); for (int i = 0; i < n; i++) { if (!std::getline(std::cin, line)) line = ""; std::cout << '[' << trim(line) << "]\n"; }}
Cmain.c · 19 줄
#include <ctype.h>#include <stdio.h>#include <stdlib.h>int main(void) { char line[512]; if (!fgets(line, sizeof line, stdin)) return 0; int n = atoi(line); for (int i = 0; i < n; i++) { if (!fgets(line, sizeof line, stdin)) line[0] = '\0'; char *start = line; while (*start && isspace((unsigned char)*start)) start++; /* 빈칸·탭·\r·\n 모두 */ char *end = start; while (*end) end++; while (end > start && isspace((unsigned char)end[-1])) end--; printf("[%.*s]\n", (int)(end - start), start); } return 0;}
Craykonmain.cray · 6 줄
n = 콘솔.줄읽기().수로변환();반복 (i = 0; i < n; i++){ line = 콘솔.줄읽기(); 콘솔.쓰기("[" + line.공백제거() + "]");}