มีฟังก์ชันสำเร็จรูปในไลบรารี ctype.h สำหรับวิเคราะห์และแปลงตัวอักษร
ฟังก์ชันวิเคราะห์ (Analysis Functions)
| Function | ตรวจสอบว่าเป็น |
|---|---|
| isalpha | ตัวอักษรภาษาอังกฤษหรือไม่ |
| isdigit | ตัวเลขหรือไม่ |
| isspace | ช่องว่าง, newline, tab |
| ispunct | สัญลักษณ์พิเศษหรือไม่ |
| islower | ตัวพิมพ์เล็ก |
| isupper | ตัวพิมพ์ใหญ่ |
| isalnum | ตัวอักษรหรือตัวเลข |
ฟังก์ชันแปลง (Converting Functions)
| Function | การแปลง |
|---|---|
| tolower() | แปลงพิมพ์ใหญ่ → พิมพ์เล็ก |
| toupper() | แปลงพิมพ์เล็ก → พิมพ์ใหญ่ |
โปรแกรม
โปรแกรม C ทดสอบประเภทตัวอักษร:
#include <stdio.h> #include <ctype.h> main(){ char character; printf("Press any key digit or alphabet\n"); character = getchar(); if (isalpha(character) > 0) printf("The character is a letter."); else if (isdigit (character) > 0) printf("The character is a digit."); else printf("The character is not alphanumeric."); }
ผลลัพธ์
Run 1: Press any key digit or alphabet 3 The character is a digit. Run 2: Press any key digit or alphabet G The character is a letter. Run 3: Press any key digit or alphabet & The character is not alphanumeric.