[C++] 알파벳 대소문자 관련 함수_ islower & isupper & tolower & toupper

반응형
반응형

[C++] islower & isupper & tolower & toupper

*개인적인 공부 내용을 기록하는 용도로 작성한 글 이기에 잘못된 내용을 포함하고 있을 수 있습니다.

_ref

<cctype> (ctype.h) - C++ Reference (cplusplus.com)

_contents

#1 islower & isupper

#2 tolower & toupper


#1 islower & isupper

islower & isupper 함수는 입력받은 문자가 소문자인지 대문자인지 판별할 때 사용하는 함수이다.

ASCII를 이용해 단순 범위 비교로 대소문자를 판별할 수 있지만, C/C++에서 제공하는 islower & isupper 함수를 사용하면 보다 깔끔하게 코드를 작성할 수 있다.

header : C <ctype.h> C++ <cctype>

 

* islower

int islower(int c)

매개변수로 들어온 문자(숫자)가 소문자인지 판별하는 함수이다.

 

_Parameter

ASCII 코드에 기반한 10진수 문자를 받는다.

 

_Return Value 

파라미터로 소문자를 받으면 0이 아닌 숫자를 반환한다. (True)

파라미터로 소문자 이외의 문자를 받으면 0을 반환한다. (False)

 

* isupper

int isupper(int c)

매개변수로 들어온 문자(숫자0가 대문자인지 판별하는 함수이다.

 

_Parameter

ASCII 코드에 기반한 10진수 문자를 받는다.

 

_Return Value

파라미터로 대문자를 받으면 0이 아닌 숫자를 반환한다. (True)

파라미터로 소문자 이외의 문자를 받으면 0을 반환한다. (False)

 

* Example

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

int main(){
	cout << "a .. islower? : " << islower('a') << endl; // true → 0이 아닌 값 return
	cout << "A .. islower? : " << islower('A') << endl; // false → 0 return
	cout << "b .. isupper? : " << isupper('b') << endl; // false → 0 return
	cout << "B .. isupper? : " << isupper('B') << endl; // true → 0이 아닌 값 return
	return 0;
}
a .. islower? : 512
A .. islower? : 0
b .. isupper? : 0
B .. isupper? : 256

#2 tolower & toupper

대문자와 소문자의 ASCII 코드 차이는 32로 대소문자에 32를 더하거나 빼주어 변환시키는 방법도 있지만, C/C++에서 제공하는 tolower, toupper 함수를 사용하면 간편하게 대소문자를 변환시킬 수 있다.

 

header : C <ctype.h> C++ <cctype>

 

* tolower

int tolower(int c)

매개변수로 들어온 문자를 소문자로 변환시켜 주는 함수이다.

_Parameter 

ASCII 코드에 기반한 10진수 문자를 받는다.

 

* toupper

int toupper(int c)

매개변수로 들어온 문자를 대문자로 변환시켜 주는 함수이다.

_Parameter

ASCII 코드에 기반한 10진수 문자를 받는다.

 

* Example

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

int main(){
	cout << "A .. tolower? : " << char(tolower('A')) << endl; // A → a 변환
	cout << "a .. tolower? : " << char(toupper('a')) << endl; // a → A 변환
	return 0;
}
A .. tolower? : a
a .. tolower? : A
반응형

댓글

Designed by JB FACTORY