본문 바로가기

Beakjoon/string

[백준] 2857번 FBI (C++) - str.find()

문제

https://www.acmicpc.net/problem/2857

 

2857번: FBI

5개 줄에 요원의 첩보원명이 주어진다. 첩보원명은 알파벳 대문자, 숫자 0~9, 대시 (-)로만 이루어져 있으며, 최대 10글자이다.

www.acmicpc.net

코드

#include <iostream>
#include <string>

using namespace std;

int main(){
  bool flag = true;
  for(int i=1; i<=5; i++){
    string s;
    cin >> s;
    if(s.find("FBI") != -1){
      cout << i << '\n';
      flag = false;
    }
  }

  if(flag){
    cout << "HE GOT AWAY!";
  }

  return 0;
}

정리

find

size_t find (const string& str, size_t pos = 0) const;

str : 찾고 싶은 문자열

pos : 탐색을 시작하는 위치(index)

특정 string에 위치를 return해주는 함수

만약 일치하는 string이 없다면 npos(-1)를 return

참조

https://blockdmask.tistory.com/338

https://www.cplusplus.com/reference/string/string/find/