091

[C#] TQC+ 문제 CS_510: 본문

Programming Language/C#

[C#] TQC+ 문제 CS_510:

공구일 2025. 5. 27. 00:08
728x90

🔍C#: CS_510.

주어진 문자열을 입력받은 숫자에 따라 write.txt에 입력하시오. 이때 null이 들어오거나 길이보다 큰 수가 들어온 경우에는 전부 다 작성하시오.

 

1. 문제 정답

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace CS_510{
    class CS_510{
        static string title = "The Little Prince";
        static string content = "To me, you are still nothing more than a little boy who is just like a hundred thousand other little boys. And I have no need of you. And you, on your part, have no need of me. To you, I am nothing more than a fox like a hundred thousand other foxes. But if you tame me, then we shall need each other. To me, you will be unique in all the world. To you, I shall be unique in all the world.";
        static void Main(string[] args){
            try{
                string? input = Console.ReadLine();
                int? num = string.IsNullOrWhiteSpace(input) ? null : int.Parse(input);
                if(num != null && num < 1) throw new Exception();

                using(StreamWriter writer = new StreamWriter("write.txt")){
                    string[] sentence = content.Split('.');
                    writer.WriteLine(title);
                    for(int i = 0; i < (num is null || num > 7 ?7:num); i++){
                        writer.WriteLine(sentence[i]+'.');
                    }
                }
            } catch{
                Console.Write("error");
            }
        }
    }
}

 

2. 정리

- 해당 값이 null인지 확인하는 법 isNullOrWhiteSpace()을 하면 됩니다. LINQ를 포함시키면 is null을 사용할 수도 있습니다.

728x90

'Programming Language > C#' 카테고리의 다른 글

[C#] TQC+ 문제 CS_602:  (0) 2025.05.27
[C#] TQC+ 문제 CS_601:  (0) 2025.05.27
[C#] TQC+ 문제 CS_509:  (0) 2025.05.26
[C#] TQC+ 문제 CS_508:  (0) 2025.05.26
[C#] TQC+ 문제 CS_507:  (0) 2025.05.26