[백준 10173] 니모를 찾아서 (C#, C++) - soo:bak
작성일 :
문제 링크https://soo-bak.github.io/algorithm/boj/findNemo-27/#문제-링크
설명https://soo-bak.github.io/algorithm/boj/findNemo-27/#설명
입력된 문자열에서 ‘nemo’라는 단어가 포함되어 있는지를 대소문자 구분 없이 판별하는 문자열 탐색 문제입니다.
- 입력은 여러 줄로 주어지며,
"EOI"
라는 단어가 등장하면 입력을 종료합니다. - 각 줄에서
'nemo'
라는 문자열이 포함되어 있는지를 검사합니다 (대소문자 구분 없음). - 포함되어 있다면
"Found"
를 출력하고, 그렇지 않다면"Missing"
을 출력합니다.
접근법https://soo-bak.github.io/algorithm/boj/findNemo-27/#접근법
- 입력을 한 줄씩 반복적으로 받아 처리합니다.
- 모든 문자를 대문자 또는 소문자로 변환한 뒤
'NEMO'
또는'nemo'
를 포함하는지 검사합니다. - 찾은 경우
"Found"
, 못 찾은 경우"Missing"
을 출력하며,"EOI"
가 등장하면 종료합니다.
Codehttps://soo-bak.github.io/algorithm/boj/findNemo-27/#code
[ C# ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
class Program {
static void Main() {
while (true) {
string line = Console.ReadLine();
if (line == "EOI") break;
string upper = line.ToUpper();
if (upper.Contains("NEMO")) Console.WriteLine("Found");
else Console.WriteLine("Missing");
}
}
}
[ C++ ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
while (true) {
string str; getline(cin, str);
if (!str.compare("EOI")) break ;
for (size_t i = 0; i < str.size(); i++) {
if (islower(str[i])) str[i] -= 32;
}
if (str.find("NEMO") != string::npos) cout << "Found\n";
else cout << "Missing\n";
}
return 0;
}