[백준 6965] Censor (C#, C++) - soo:bak
작성일 :
문제 링크
설명
각 줄에서 길이가 4인 단어를 모두 “**“로 바꾸고, 줄 사이에 빈 줄을 넣는 문제입니다.
접근법
먼저 한 줄을 공백 기준으로 단어로 나눕니다.
다음으로 길이가 4인 단어는 “**“로 바꿔 다시 합칩니다.
마지막으로 각 줄을 출력하고, 줄 사이에 빈 줄을 추가합니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Text;
class Program {
static void Main() {
var n = int.Parse(Console.ReadLine()!);
var sb = new StringBuilder();
for (var i = 0; i < n; i++) {
var line = Console.ReadLine()!;
var parts = line.Split(' ');
for (var j = 0; j < parts.Length; j++) {
if (parts[j].Length == 4) parts[j] = "****";
}
sb.AppendLine(string.Join(" ", parts));
if (i != n - 1) sb.AppendLine();
}
Console.Write(sb);
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
string line; getline(cin, line);
for (int i = 0; i < n; i++) {
getline(cin, line);
stringstream ss(line);
string word;
string out;
bool first = true;
while (ss >> word) {
if (!first) out.push_back(' ');
if ((int)word.size() == 4) out += "****";
else out += word;
first = false;
}
cout << out << "\n";
if (i != n - 1) cout << "\n";
}
return 0;
}