How to use isalnum function in C programming?

The isalnum function in C programming checks whether the argument passed is an alphanumeric character (alphabet or number) or not. It is declared in ctype.h and takes one argument. The isalnum function tests for any character for which isalpha or isdigit is true.

 

Syntax of isalnum function in C:

//Syntax of isalnum

int isalnum(int c);

Parameters:

c => character to classify

Return value:

Non-zero value => If the argument is an alphanumeric character.
0 => If the argument is neither an alphabet nor a digit.

 

Example,

Input : 3
Output : Non-zero value


Input : b
Output : Non-zero value


Input : @
Output : Zero

 

C program to check if a character is an alphanumeric character using isalnum():

Let’s see a C program to check given character is alphanumeric or not.

#include <stdio.h>
#include <ctype.h>

int main()
{
    int c;
    printf("Enter a Valid Character: ");
    c = fgetc(stdin);

    if (isalnum(c) == 0)
    {
        printf("%c is not an alphanumeric character.", c);
    }
    else
    {
        printf("%c is an alphanumeric character.", c);
    }

    return 0;
}

Output1:

Enter a character: 3
3 is an alphanumeric character.

Output2:

Enter a character: @
@ is not an alphanumeric character.

 

Note: If the argument’s value (c) is neither representable as unsigned char not equal to EOF, the behavior of isalnum is undefined.

 

As we know the behavior of isalnum is undefined if the argument’s value is neither representable as unsigned char nor equal to EOF. So to use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char. Because it is good to convert signed char to unsigned char before being assigned or converted to a larger signed type.

int my_isalnum(char ch)
{
    return isalnum((unsigned char)ch);
}

 

 

There are many applications of isalnum in C programming. But to find out the number of alphanumeric in a given input stream is very popular. So let’s see a C code to find the count of the alphanumeric characters in the given input stream.

#include <ctype.h>
#include<string.h>
#include <stdio.h>

unsigned int findCountAlnuminGivenStream(char *str)
{
    unsigned int counter = 0;
    if (str != NULL)
    {
        unsigned int i = 0;
        // counting of alphanumerics
        while (str[i] != '\0')
        {
            if (isalnum((unsigned char)str[i]))
            {
                ++counter;
            }
            ++i;
        }
    }
    // returning total number of alphanumerics
    // present in given input stream
    return (counter);
}

int main()
{
    char str[] = "admin@aticleworld.com";

    unsigned int counter = findCountAlnuminGivenStream(str);


    printf("Total number of char in input stream is : %u\n\n", strlen(str));

    printf("\nNumber of alphanumerics in the "
           "given input stream is : %u\n\n", counter);

    return 0;
}

Output:

Total number of char in input stream is : 21

Number of alphanumerics in the given input stream is : 19

 

Recommended Post:

Leave a Reply

Your email address will not be published. Required fields are marked *