測試字元型別的C程式


"ctype.h"庫中提供了一些預定義函式,用於分析字元輸入並進行轉換。

分析函式

以下是字元分析函式列表:

函式檢查輸入字元是否為
isalpha字母
isdigit數字
isspace空格、換行符或製表符
ispunct特殊符號
islower小寫字母
isupper大寫字母
isalnum字母或數字

轉換函式

以下是轉換函式列表:

函式轉換
tolower()將大寫字母轉換為小寫字母
toupper()將小寫字母轉換為大寫字母

程式

以下是用於測試字元型別的C程式,它使用了字元分析和轉換函式:

線上演示

#include <stdio.h>
#include <ctype.h>
main(){
   char character;
   printf("Press any key digit or alphabet
");    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.

更新於:2021年3月26日

1K+ 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.