본문 바로가기

프로그래밍/C

[library] isalpha 구현하기

📌 매뉴얼 (Linux)

더보기

SYNOPSIS

       int isalpha(int c);

: 함수 프로토타입

 

DESCRIPTION

       isalpha()
              checks  for  an  alphabetic  character; in the standard "C" locale, it is equivalent to
              (isupper(c) || islower(c)).  In some locales, there may be  additional  characters  for
              which isalpha() is true—letters which are neither uppercase nor lowercase.

: isupper (영어 대문자인지 판별), islower (영어 소문자인지 판별) 둘 중 하나라도 만족하면 isalpha가 true

 

RETURN VALUE
       The  values  returned  are nonzero if the character c falls into the tested class, and zero if
       not.

: 반환값은 조건에 만족하지 않으면 0이고, 만족하면 0이 아닌 값이다

 

NOTES
       The standards require that the argument c for these functions is either EOF or a value that is
       representable in the type unsigned char.  If the argument c is of type char, it must  be  cast
       to unsigned char, as in the following example:

           char c;
           ...
           res = toupper((unsigned char) c);

       This  is  necessary  because  char  may be the equivalent of signed char, in which case a byte
       where the top bit is set would be sign extended when converting to int, yielding a value  that
       is outside the range of unsigned char.

       The details of what characters belong to which class depend on the locale.  For example, isup‐
       per() will not recognize an A-umlaut (Ä) as an uppercase letter in the default C locale.

 

📌 작성 코드

int do_isalpha(int c)
{
    if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
        return (1);
    else
        return (0);
}

 

📌 코드 리뷰

단순하게 접근해서 'A' ~ 'Z' 거나 'a' ~ 'z' 에 c 값이 존재하면 return을 1로, 그 외의 경우에는 return을 0으로 설정하였다.

return value를 if문 조건에 있는 '(c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')' 로 넣어도 동일하게 동작한다! 

 

 

'프로그래밍 > C' 카테고리의 다른 글

[library] isalnum 구현하기  (0) 2024.03.13
[study] 구조체 & 구조체 포인터  (0) 2024.03.13
[study] file descriptor 와 open()  (0) 2024.03.12
[study] 함수 포인터  (1) 2024.03.11
[library] isdigit 구현하기  (0) 2024.02.29
Recent Posts
Popular Posts
Recent Comments