[c++] 문자열에서 알파벳의 개수 세는 방법
우선, std::string
클래스를 사용하여 문자열에서 알파벳의 개수를 카운트할 수 있습니다. 아래는 간단한 코드 예시입니다.
#include <iostream>
#include <string>
#include <cctype>
int main() {
std::string input = "Hello, World!";
int alphabetCount = 0;
for (char c : input) {
if (std::isalpha(c)) {
alphabetCount++;
}
}
std::cout << "알파벳 개수: " << alphabetCount << std::endl;
return 0;
}
이 코드는 문자열에서 알파벳인지를 판별하는 std::isalpha
함수를 사용하여 알파벳의 개수를 카운트합니다.
참고문헌:
- http://www.cplusplus.com/reference/string/string/
- http://www.cplusplus.com/reference/cctype/isalpha/