[백준/C++] 10809번: 알파벳 찾기

꿈별·2022년 11월 7일
0

문제풀이

목록 보기
4/52

문제


풀이

find()로 찾은 문자가 처음 등장하는 위치의 숫자int형으로 변환되는 것에 주의한다.

#include <iostream>
#include <string>
using namespace std;

int main(void){
	string S = "";
	cin >> S;
	for (int i = 97; i <= 122; i ++)
	{
		cout << (int)S.find(i) << " ";
	}
}

참고

  • char형 숫자 int형으로 변환
    -> (int)S.find(j)
    ❗ 변환하지 않고, 없는 문자를 찾으려고 할 경우 string::npos를 반환한다,
    -> string::npos는 string 클래스에 static const size_type= -1 로 명시되어 있지만, string::npos의 자료형이 unsinged이기 때문에 2의 보수 개념에 의해 표현할 수 있는 최대 크기의 양수가 출력된다.
    https://cryptosalamander.tistory.com/11
  • 소문자 알파벳의 ASCII 코드
    -> a~z : 97~122
    https://codedragon.tistory.com/2547

0개의 댓글