The first line gives the number of lines n. Then n lines of text follow. For each line remove all whitespace at the start and at the end, and print the result wrapped in square brackets, like "[result]", on its own line. Whitespace means spaces, tabs, and a \r at the end of the line (the input file may use Windows line endings \r\n). Whitespace between characters stays as it is. If the line is empty or only whitespace, print "[]".
Input
1 ≤ n ≤ 100. Each line consists of spaces, tabs and ASCII symbols, letters and digits, length 0 to 300 excluding the line ending
Output
n lines, each [trimmed text]
Example 1 · stdin
3
hello
world
keep inner
stdout
[hello]
[world]
[keep inner]
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
Trimming both ends: Python s.strip(), JS/TS s.trim(), Java s.trim() (every code ≤ 32) or s.strip() (Unicode whitespace), C# s.Trim(), Craykon s.Trim(). C and C++ have no trim: in C narrow both ends with isspace; in C++ find the positions with find_first_not_of(" \t\r\n")/find_last_not_of and substr. The Windows line-ending trap: whether reading a line removes the \r differs. Java readLine and C# ReadLine strip the whole \r\n (the Craykon console removes \r too), while Python input(), C fgets, C++ getline and JS split("\n") keep the \r. So in C++, leaving \r out of the characters to strip keeps the \r at the end, and the spaces or tabs before it stay too ("hi\t\r" → "[hi\t\r]"). Other traps: naming only the space, as in Python strip(" "), leaves tabs and \r, and " ".join(s.split()) also squeezes the inner whitespace into one space.
Pythonmain.py · 3 lines
n = int(input())for _ in range(n): print("[" + input().strip() + "]")
JavaScriptmain.js · 4 lines
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 lines
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 lines
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 lines
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 lines
#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 lines
#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 lines
n = Console.ReadLine().ToNumber();for (i = 0; i < n; i++){ line = Console.ReadLine(); Console.WriteLine("[" + line.Trim() + "]");}