C/C++ 中的 isalpha() 和 isdigit()
isalpha()
函式 isalpha() 用於檢查字元是否為字母。此函式在 ctype.h 標頭檔案 中宣告。如果引數是字母,則返回整數值;否則,返回零。
以下是 C 語言中 isalpha() 的語法:
int isalpha(int value);
這裡:
value − 這是一個整型型別的單個引數。
以下是一個 C 語言中 isalpha() 的示例:
示例
#include<stdio.h> #include<ctype.h> int main() { char val1 = 's'; char val2 = '8'; if(isalpha(val1)) printf("The character is an alphabet\n"); else printf("The character is not an alphabet\n"); if(isalpha(val2)) printf("The character is an alphabet\n"); else printf("The character is not an alphabet"); return 0; }
輸出
輸出結果如下:
The character is an alphabet The character is not an alphabet
isdigit()
函式 isdigit() 用於檢查字元是否為數字字元。此函式在 "ctype.h" 標頭檔案中宣告。如果引數是數字,則返回整數值;否則,返回零。
以下是 C 語言中 isdigit() 的語法:
int isdigit(int value);
這裡:
value − 這是一個整型型別的單個引數。
以下是一個 C 語言中 isdigit() 的示例:
示例
#include<stdio.h> #include<ctype.h> int main() { char val1 = 's'; char val2 = '8'; if(isdigit(val1)) printf("The character is a digit\n"); else printf("The character is not a digit\n"); if(isdigit(val2)) printf("The character is a digit\n"); else printf("The character is not a digit"); return 0; }
輸出
輸出結果如下:
The character is not a digit The character is a digit
廣告