Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,700 questions

57,013 answers

573 users

How to use the isalnum function to check if a character is alphanumeric in C

1 Answer

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

/**
 * Checks whether a given character is alphanumeric.
 * 
 * @param c The character to evaluate.
 * @return Non-zero (true) if alphanumeric, 0 (false) otherwise.
 */
int check_alphanumeric(char c) {
    // Cast to unsigned char to avoid undefined behavior with signed chars
    return isalnum((unsigned char)c) != 0;
}

int main(void) {
    // Test cases containing letters, digits, and special characters
    char test_characters[] = {'G', '5', 'a', '#', ' ', '\n'};
    int total_chars = sizeof(test_characters) / sizeof(test_characters[0]);

    for (int i = 0; i < total_chars; i++) {
        char current = test_characters[i];
        
        // Using our custom wrapper function
        if (check_alphanumeric(current)) {
            printf("Character '%c' (ASCII: %d) is alphanumeric.\n", current, current);
        } else {
            printf("Character '%c' (ASCII: %d) is NOT alphanumeric.\n", current, current);
        }
    }

    return 0;
}


/*
run:

Character 'G' (ASCII: 71) is alphanumeric.
Character '5' (ASCII: 53) is alphanumeric.
Character 'a' (ASCII: 97) is alphanumeric.
Character '#' (ASCII: 35) is NOT alphanumeric.
Character ' ' (ASCII: 32) is NOT alphanumeric.
Character '
' (ASCII: 10) is NOT alphanumeric.

*/

 



answered 3 days ago by avibootz
...