언어/Basic C++

[Basic C++] #56_cctype, 대문자+소문자 확인

Hardii2 2022. 9. 15. 19:30

 

[Basic C++] #56_cctype, 대문자+소문자 확인

C++의 라이브러리 중 "cctype"에 대해 알아보겠습니다.

cctype 헤더가 제공하는 소문자+대문자 판별 함수에 대한 내용입니다.

 

 


 

isdigit, isupper, islower, isspace

 

1. 헤더

#include <cctype>

 

2. 코드

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

int main()
{
	string str = "aBF c12";

	cout << str << endl;

	for (int i = 0; i < str.size(); i++)
	{
		// 1. isdigit : 숫자 문자 구분
		if (isdigit(str[i]))
			cout << str[i] << " : 숫자" << '\n';
		// 2. isspace : 공백 구분
		else if(isspace(str[i]))
			cout << i << " : 공백" << '\n';

		// 3. isupper : 대분자 구분
		else if (isupper(str[i]))
			cout << str[i] << " : 대문자" << '\n';

		// 4. islower : 소문자 구분
		else if (islower(str[i]))
			cout << str[i] << " : 소문자" << '\n';

		// 5. isalpha : 알파벳 구분
		else if (isalpha(str[i]))
			cout << str[i] << " : 알파벳" << '\n';
	}

	return 0;
}
// 에러!!!
if(islower(s[i]) == true)
	cout << "is lower!" << endl;​

* 주의할 점: isupper(), islower() 등의 cctype 제공 함수들은 리턴 타입이 "int"입니다!