Search code examples
ccs50alphanumeric

How could alphanumeric be a character and not a string?


In CS50 library there is function named isalnum that check whether a character is alphanumeric. However, that function only accepts a char not a c-string (i.e., char[]).

By alphanumeric, I understand that is something as for example "7x", so it is two characters, one alphabetic and one numeric, so "7x" is a string not a char because it consist of the characters "7" and "x".

Now how could the function isalnum check if a single character is alphanumeric ??


Solution

  • From https://manual.cs50.io/3/isalnum

    Function int isalnum(char c);

    This function checks whether c is alphanumeric (i.e., a letter or a number) or not.

    So it checks if is either a letter or a number not both simultaneously (e.g., a1). Therefore, the parameter does not have to be a String, a char will be enough. With a char you can test single digits from '0 to 9' as well single letters, hence no need for Strings (or char[]).

    The function will return 1 when the char c is either a letter form 'a to Z' or a digit from '0 to 9', 0 otherwise.