Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

ฟังก์ชัน isalnum() ใน C: ตรวจสอบตัวอักษรและตัวเลข

ฟังก์ชัน isalnum() ตรวจสอบว่าตัวอักษรเป็น Alphanumeric (ตัวอักษร A-Z, a-z หรือ ตัวเลข 0-9) หรือไม่ อยู่ใน Header <ctype.h>

ไวยากรณ์

#include <ctype.h>
int isalnum(int c);
  • Parameter: int c - ตัวอักษรที่ตรวจสอบ (ต้องเป็น unsigned char หรือ EOF)
  • คืนค่า: Non-zero (True) ถ้าเป็น Alphanumeric, 0 (False) ถ้าไม่ใช่

ตัวอย่าง

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

int main() {
    char val1 = 's';  // Letter
    char val2 = '8';  // Digit
    char val3 = '$';  // Symbol
    
    if (isalnum(val1))
        printf("'%c' is alphanumeric\n", val1);
    else
        printf("'%c' is NOT alphanumeric\n", val1);
    
    if (isalnum(val2))
        printf("'%c' is alphanumeric\n", val2);
    else
        printf("'%c' is NOT alphanumeric\n", val2);
    
    if (isalnum(val3))
        printf("'%c' is alphanumeric\n", val3);
    else
        printf("'%c' is NOT alphanumeric\n", val3);
    
    return 0;
}

ผลลัพธ์

's' is alphanumeric
'8' is alphanumeric
'$' is NOT alphanumeric

ความสัมพันธ์กับฟังก์ชันอื่นๆ

isalnum(c)  ≡  isalpha(c) || isdigit(c)

isalpha(c)  ≡  isupper(c) || islower(c)

ตัวอักษรที่ isalnum() คืน True (C Locale)

Letters:  A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
          a b c d e f g h i j k l m n o p q r s t u v w x y z
Digits:   0 1 2 3 4 5 6 7 8 9
Total: 26 + 26 + 10 = 62 Characters

Locale Dependency

ใน Locale อื่น (เช่น Thai, Greek, Cyrillic) isalnum() อาจคืน True สำหรับตัวอักษรในภาษานั้นๆ

setlocale(LC_CTYPE, "th_TH.UTF-8");
// isalnum('ก') อาจคืน True (ขึ้นกับ Implementation)

การใช้งานจริง: Input Validation

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

bool isValidUsername(const char *username) {
    if (!username || strlen(username) == 0) return false;
    
    for (size_t i = 0; username[i]; i++) {
        unsigned char c = (unsigned char)username[i];
        if (!isalnum(c) && c != '_') {  // Allow underscore
            return false;
        }
    }
    return true;
}

int main() {
    printf("user123: %s\n", isValidUsername("user123") ? "Valid" : "Invalid");   // Valid
    printf("user@123: %s\n", isValidUsername("user@123") ? "Valid" : "Invalid"); // Invalid
    printf("user_123: %s\n", isValidUsername("user_123") ? "Valid" : "Invalid"); // Valid
    return 0;
}

ข้อควรระวัง: Undefined Behavior

// ผิด: char อาจเป็น Signed
char c = getchar();
isalnum(c);  // UB ถ้า c < 0

// ถูกต้อง:
int c = getchar();
isalnum(c);  // Safe

// หรือ Cast
char c = getchar();
isalnum((unsigned char)c);  // Safe

C++ Version ()

#include <cctype>
#include <locale>

int main() {
    char c = 'A';
    std::locale loc;
    
    // C-style
    std::isupper(c);
    
    // C++ Locale-aware (Overload)
    std::isupper(c, loc);
    
    return 0;
}

สรุป

isalnum() ตรวจสอบ Alphanumeric (Letters + Digits) คืน Non-zero/0 ต้อง Cast เป็น unsigned char ใช้สำหรับ Input Validation, Parsing, Tokenization เป็นส่วนหนึ่งของ ctype.h Family