본문 바로가기

프로그래밍/C

[library] isalnum 구현하기

📌 매뉴얼 (Linux)

더보기

SYNOPSIS

       int isalnum(int c);

: 함수 프로토타입

 

DESCRIPTION

       isalnum()
              checks for an alphanumeric character; it is equivalent to (isalpha(c) || isdigit(c)).

: isalpha()와 isdigit() 둘 중 하나라도 만족하면 isalnum()이 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_isalnum(int c)
{
	if (c >= '0' && c <= '9')
		return (1);
	else if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
		return (1);
	else
		return (0);
}

 

📌 코드 리뷰

이전과 마찬가지로 number 부분은 '0' ~ '9' 사이의 값으로, alphabet 부분은 'A' ~ 'Z' , 'a' ~ 'z' 사이 값으로

범위를 설정해서 if문에 작성해주었다. 물론 이 부분을 do_isalpha(c) || do_isdigit(c)로 이전에 만든 두 함수를 활용할

수도 있지만, 따로 함수를 불러오지 않고 만들어보았다.

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

[library] isascii 구현하기  (0) 2024.03.18
[study] Linked List (연결 리스트)  (0) 2024.03.14
[study] 구조체 & 구조체 포인터  (0) 2024.03.13
[study] file descriptor 와 open()  (0) 2024.03.12
[study] 함수 포인터  (1) 2024.03.11
Recent Posts
Popular Posts
Recent Comments