![[C++] 백준 5단계 - 10809번 문제](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdna%2FbaplLX%2Fbtr7gycvVpl%2FAAAAAAAAAAAAAAAAAAAAAEa2bx3VlN0PXykQ2V1dSF5VrDggTA-ltEdBKD_5c-mz%2Fimg.png%3Fcredential%3DyqXZFxpELC7KVnFOS48ylbz2pIh7yKj8%26expires%3D1753973999%26allow_ip%3D%26allow_referer%3D%26signature%3Dn1ajnM5C9MDMncEZrVXAH1H7ge4%253D)
[C++] 백준 5단계 - 10809번 문제자료구조 & 알고리즘/BOJ2023. 4. 3. 01:10
Table of Contents
문제설명
소스코드
#include <iostream>
using namespace std;
int main()
{
string input; string alphabet = "abcdefghijklmnopqrstuvwxyz";
cin >> input;
for (int i = 0; i < alphabet.length(); ++i)
{
if (input.find(alphabet[i]) != string::npos)
{
cout << input.find(alphabet[i]) << " ";
}
else cout << -1 << " ";
}
}
풀이
string형의 find()함수를 활용하여 풀었다.
- string 클래스의 멤버함수이며, find("찾고자 하는 문자 또는 문자열")로 사용한다.
- 리턴값은 찾는 문자의 첫번째 인덱스값이다.
- 찾는 문자가 없다면 npos를 리턴하는데 쓰레기값이다.
'자료구조 & 알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 5단계 - 1152번 문제 (0) | 2023.04.05 |
---|---|
[C++] 백준 5단계 - 2675번 문제 (0) | 2023.04.03 |
[C++] 백준 5단계 - 11720번 문제 (0) | 2023.04.03 |
[C++] 백준 5단계 - 11654번 문제 (0) | 2023.04.02 |
[C++] 백준 5단계 - 9086번 문제 (0) | 2023.04.02 |