091

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

Programming Language/C#

[C#] TQC+ 문제 CS_507:

공구일 2025. 5. 26. 20:05
728x90

🔍C#: CS_507.

주어진 read.txt에서 주어진 문자열에 입력한 단어가 얼마나 들어있는지 찾으시오.

 

1. 문제 정답

using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;

namespace CS_507{
    class CS_507{
        static string RfilePath = "read.txt";
        //static string WfilePath = "write.txt";
        static void Main(string[] args){
            try{
                string input = Console.ReadLine() ?? "";
                using(StreamReader reader = new StreamReader(RfilePath))
                {
                    string line = reader.ReadLine();
                    int index = 0, count = 0;
                    while(index < line.Length){
                        if(line.Substring(index).StartsWith(input)){
                            index += input.Length;
                            count ++;
                        }else index++;
                    }
                    Console.Write(count);
                }
            } catch{
                Console.Write("error");
            }
        }
    }
}

 

2. 정리

- 단어의 개수를 세는 문제가 아닌 문자열 내에서 해당 문자열이 몇번 등장하고 나오는지 세는 것이기 때문에 Substring으로 잘라서 해당 입력된 것을 시작하는 StartWith으로 세주고 해당 단어로 시작할 경우에는 개수를 센뒤 최종적으로 계산해주면 됩니다.

728x90

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

[C#] TQC+ 문제 CS_509:  (0) 2025.05.26
[C#] TQC+ 문제 CS_508:  (0) 2025.05.26
[C#] TQC+ 문제 CS_506:  (0) 2025.05.26
[C#] TQC+ 문제 CS_505:  (0) 2025.05.26
[C#] TQC+ 문제 CS_504:  (0) 2025.05.26