ft_isalnum

Subject

ISALNUM(3) (simplified)

NAME
    isalnum -- alphanumeric character test
SYNOPSIS
    int isalnum(int c)
DESCRIPTION
    The isalnum() function tests for any character for which isalpha(3) or isdigit(3) is true. The value of the argument must be representable as an unsigned char or the value of EOF.
RETURN VALUES
    The isalnum() function returns zero if the character tests false and returns non-zero if the character tests true.

Understandable explanation

For this function, the man is self-explanatory, but I'll still explain it in other words.

The isalnum() function returns a non-zero value if the character passed as an int parameter is alphabetical or a decimal digit character.

If the character is not alphabetical nor a decimal digit character, the isalnum() function returns 0.

Hints

ft_isalnum.c
int    ft_isalnum(int c)
{
    if (/* c isalpha or c isdigit */)
        return (/* non-zero value of your choice */);
    return (0);
}

Commented solution

Come on ! You really need the code for that function ?

ft_isalnum (variant 1)
ft_isalnum.c
#include "libft.h"

int    ft_isalnum(int c)
{
    /* This checks makes use of the 2 preceeding functions we built */
    if (ft_isalpha(c) || ft_isdigit(c))
        return (c); //If we reach this point we can return c as it will be a non-zero value
    return (0);
}
ft_isalnum (variant 2)
ft_isalnum.c
#include "libft.h"

int    ft_isalnum(int c)
{
    /* This check is the same as the variant one but without using the pre-existing functions, it's longer to write, it could be useful if you don't want to have it being dependant on other functions */
    if ((c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122))
        return (c); // If we reach thi point we can simply return c as it will be a non-zero value
    return (0);
}

Last updated