### C++11에서 문자열의 알파벳 여부 판단하기
C++11에서는 문자열이 알파벳(A-Z, a-z) 문자로만 이루어졌는지 확인하기 위해 'std::regex'를 사용할 수 있습니다. 이 방법을 통해 문자열이 특정 조건을 만족하는지 쉽게 검사할 수 있습니다.
#### 코드 예시
```
#include
#include
#include
int main() {
std::string str = "HelloWorld";
// 알파벳 문자만으로 이루어진 문자열 검사 (A-Z, a-z)
std::regex alphaRegex("^[A-Za-z]+$");
if (std::regex_match(str, alphaRegex)) {
std::cout << "The string contains only alphabetic characters." << std::endl;
} else {
std::cout << "The string contains non-alphabetic characters." << std::endl;
}
return 0;
}
```
코드 설명
- std::regex alphaRegex("^[A-Za-z]+$");: 대소문자 알파벳(A-Z, a-z)만 포함된 문자열을 검사하는 정규 표현식을 정의합니다.
- std::regex_match(str, alphaRegex): 문자열이 정규 표현식과 일치하는지 확인합니다.
- 이 코드는 문자열이 알파벳 문자로만 이루어졌는지 판단하며, 알파벳 문자만 포함된 경우에는 "The string contains only alphabetic characters."를 출력합니다.
댓글
댓글 쓰기