42Seoul/Libft

[C] 나만의 라이브러리 - atoi, isalpha, isdigit, isalnum, isascii, isprint, toupper, tolower

  • -
728x90

1. atoi

int my_atoi(const char *str);

주어지는 문자열에 연속되는 숫자가 있을 때, 그 숫자를 int형으로 바꾸는 함수

int    my_atoi(const char *str)
{
    long    sign;
    long    res;

    while ((*str >= 9 && *str <= 13) || *str == ' ')
        str++;
    sign = 1;
    res = 0;
    if (*str == '+' || *str == '-')
    {
        if (*str == '-')
            sign = -1;
        str++;
    }
    while (*str && my_isdigit(*str))
    {
        res = res * 10 + (*str - '0');
        if (sign == -1 && res > 2147483648)
            return (0);
        if (sign == 1 && res > 2147483647)
            return (-1);
        str++;
    }
    return (sign * res);
}
  • 공백(white space) 제거
  • + or - 가 있을 때 부호 지정 (연속 된다면 0 반환)
  • 연속되는 문자형 숫자를 int형으로 변환 후 부호와 값을 곱하여 반환

2. isalpha

int my_isalpha(int c);

매개변수값이 알파벳의 범위일 때 true 반환, 그렇지 않으면 false 반환

int    my_isalpha(int c)
{
    return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
}

3. isdigit

int my_isdigit(int c);

매개변수값이 숫자의 범위일 때 true 반환, 그렇지 않으면 false 반환

int    my_isdigit(int c)
{
    return (c >= '0' && c <= '9');
}

4. isalnum

int my_isalnum(int c);

매개변수값이 숫자 또는 알파벳의 범위일 때 true 반환, 그렇지 않으면 false 반환

int    my_isalnum(int c)
{
    return (my_isalpha(c) || my_isdigit(c));
}

5. isascii

int my_isascii(int c);

매개변수값이 ascii의 범위일 때 true 반환, 그렇지 않으면 false 반환

int    my_isascii(int c)
{
    return (c >= 0 && c <= 127);
}

6. isprint

int my_isprint(int c);

매개변수값이 출력가능한 문자일 때 true 반환, 그렇지 않으면 false 반환

int    my_isprint(int c)
{
    return (c >= ' ' && c < 127);
}

7. isprint

int my_isprint(int c);

매개변수값이 출력가능한 문자일 때 true 반환, 그렇지 않으면 false 반환

int    my_isprint(int c)
{
    return (c >= ' ' && c < 127);
}

8. tolower

int my_tolower(int c);

매개변수 값이 대문자일 때 소문자로 변환, 그렇지않으면 그대로 반환

int    my_tolower(int c)
{
    if (c >= 'A' && c <= 'Z')
        return (c - 'A' + 'a');
    return (c);
}

9. toupper

int my_toupper(int c);

매개변수 값이 소문자 때 대문자로 변환, 그렇지않으면 그대로 반환

int    my_toupper(int c)
{
    if (c >= 'a' && c <= 'z')
        return (c - 'a' + 'A');
    return (c);
}
728x90
300x250
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.