복기
C#의 String.Split()은 공백 문자 전체를 제거하여 주지는 않으니 유의하자.
코드
#include <iostream> #include <string> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); string word; int lineSize = 0; while (cin >> word) { if (word == "<br>") { cout << '\n'; lineSize = 0; continue; } else if (word == "<hr>") { if (lineSize != 0) cout << '\n'; cout << "--------------------------------------------------------------------------------\n"; lineSize = 0; continue; } if (lineSize + word.size() > 80) { cout << '\n'; lineSize = 0; } lineSize += word.size() + 1; cout << word << ' '; } } |
using System; using System.IO; using System.Text; namespace Csharp { class Program { static StringBuilder sb = new StringBuilder(); static int lineSize = 0; static void Main(string[] args) { while (true) { var words = Console.ReadLine()?.Split(); if (words == null) break; for (int i = 0; i < words.Length; i++) { if (words[i] == "") continue; else if (words[i] == "<br>") { StartNewLine(); continue; } else if (words[i] == "<hr>") { if (lineSize != 0) StartNewLine(); sb.Append("--------------------------------------------------------------------------------"); StartNewLine(); continue; } if (lineSize + words[i].Length > 80) StartNewLine(); sb.Append($"{words[i]} "); lineSize += words[i].Length + 1; } } Console.WriteLine(sb.ToString()); } static void StartNewLine() { sb.Append('\n'); lineSize = 0; } } } |
from sys import stdin, stdout print = stdout.write lineSize = 0 for line in stdin.readlines(): for word in line.split(): if word == '<br>': print('\n') lineSize = 0 continue if word == '<hr>': if lineSize != 0: print('\n') print(f"{'-' * 80}\n") lineSize = 0 continue if lineSize + len(word) > 80: print('\n') lineSize = 0 print(f"{word} ") lineSize += len(word) + 1 |