isalnum() function in C Language

isalnum() function in C programming language checks whether the given character is alphanumeric or not. isalnum() function defined in ctype.h header file.

Alphanumeric: A character that is either a letter or a number.
Syntax:

int isalnum(int x);

Examples:

Input : 1
Output : Entered character is alphanumeric
Input : A
Output : Entered character is alphanumeric
Input : &
Output : Entered character is not alphanumeric
filter_none

edit
close

play_arrow

link
brightness_4
code

// C code to illustrate isalphanum()
#include <ctype.h>
#include <stdio.h>
  
int main()
{
    char ch = 'a';
  
    // checking is it alphanumeric or not?
    if (isalnum(ch))
        printf("\nEntered character is alphanumeric\n");
    else
        printf("\nEntered character is not alphanumeric\n");
}

chevron_right


Output:

Entered character is alphanumeric

Application: isalnum() function is used to find out number of alphanumeric in a given sentence(or any input).
Example:

Input: abc123@
Output: Number of alphanumerics in the given input is : 6
Input: a@#
Output: Number of alphanumerics in the given input is : 1
Input: ...akl567
Output: Number of alphanumerics in the given input is : 6
filter_none

edit
close

play_arrow

link
brightness_4
code

// C code to illustrate isalphanum()
#include <ctype.h>
#include <stdio.h>
  
int ttl_alphanumeric(int i, int counter)
{
    char ch;
    char a[50] = "www.geeksforgeeks.org";
    ch = a[0];
  
    // counting of alphanumerics
    while (ch != '\0') {
        ch = a[i];
        if (isalnum(ch))
            counter++;
  
        i++;
    }
  
    // returning total number of alphanumerics
    // present in given input
    return (counter);
}
  
int main()
{
    int i = 0;
  
    int counter = 0;
    counter = ttl_alphanumeric(i, counter);
  
    printf("\nNumber of alphanumerics in the "
           "given input is : %d", counter);
    return 0;
}

chevron_right


Output:

Number of alphanumerics in the given input is : 19


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.