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

โปรแกรม C ทดสอบประเภทตัวอักษร (Character Type Testing)

มีฟังก์ชันสำเร็จรูปในไลบรารี 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.