본문 바로가기
프로그래밍 언어/C++

C++ 문자 대소문자 판별 & 변환 함수 정리 (isupper, islower, toupper, tolower)

by Jo_Wick 2025. 9. 30.
반응형

1. 대문자 / 소문자 판별

1. isupper

int isupper(int ch);

 

  • ch가 대문자 알파벳(A~Z)이면 0이 아닌 값(true)을 반환
  • 아니면 0(false) 반환

2. islower

int islower(int ch);
  • ch가 소문자 알파벳(a~z)이면 0이 아닌 값(true) 반환
  • 아니면 0(false) 반환
#include <iostream>
#include <cctype>
using namespace std;

int main() {
    char a = 'A', b = 'z', c = '1';

    cout << isupper(a) << endl; // 1 (true)
    cout << isupper(b) << endl; // 0
    cout << islower(b) << endl; // 1 (true)
    cout << islower(c) << endl; // 0
}

 

2. 대문자 / 소문자 변환

1. toupper

int toupper(int ch);
  • 소문자면 대문자로 변환된 값 반환
  • 그 외 문자는 변화 없이 그대로 반환

2. tolower

int tolower(int ch);
  • 대문자면 소문자로 변환된 값 반환
  • 그 외 문자는 변화 없음
#include <iostream>
#include <cctype>
using namespace std;

int main() {
    char lower = 'h';
    char upper = 'Q';
    char digit = '5';

    cout << (char)toupper(lower) << endl; // H
    cout << (char)tolower(upper) << endl; // q
    cout << (char)tolower(digit) << endl; // 5 (변화 없음)
}

 


※ toupper, tolower 모두 반환값이 int 형 정수이기 때문에 무조건 형변환을 시켜야 제대로 출력이 된다

반응형

댓글