在本教程中,我們將借助示例了解 C++ isalpha() 函數。
C++ 中的isalpha() 函數檢查給定字符是否為字母表。它在cctype 頭文件中定義。
示例
#include <iostream>
#include <cctype>
using namespace std;
int main() {
// check if '27' is an alphabet
int result = isalpha('27');
cout << result;
return 0;
}
// Output: 0
isalpha() 語法
用法:
isalpha(int ch);
在這裏,ch 是我們要檢查的字符。
參數:
isalpha() 函數采用以下參數:
- ch- 要檢查的字符,投射到
int或者EOF
返回:
isalpha() 函數返回:
- 如果
ch是字母表,則為非零值 - 如果
ch不是字母則為零
isalpha() 原型
cctype 頭文件中定義的isalpha() 原型為:
int isalpha(int ch);
在這裏,ch 會檢查當前安裝的 C 語言環境分類的字母。默認情況下,以下字符是字母:
- 大寫字母:
'A'到'Z' - 小寫字母:
'a'到'z'
isalpha() 未定義行為
isalpha() 的行為在以下情況下未定義:
ch的值不能表示為unsigned char,或者ch的值不等於EOF
示例:C++ isalpha()
#include <cctype>
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[] = "ad138kw+~!$%?';]qjj";
int count = 0, check;
// loop to count the no. of alphabets in str
for (int i = 0; i <= strlen(str); ++i) {
// check if str[i] is an alphabet
check = isalpha(str[i]);
// increment count if str[i] is an alphabet
if (check)
++count;
}
cout << "Number of alphabet characters: " << count << endl;
cout << "Number of non-alphabet characters: " << strlen(str) - count;
return 0;
}
輸出
Number of alphabet characters: 7 Number of non alphabet characters: 12
在這個程序中,我們使用了 for 循環和 isalpha() 函數來計算 str 中的字母數量。
該程序中使用了以下變量和代碼:
strlen(str)- 給出str字符串的長度check- 在isalpha()的幫助下檢查str[i]是否是字母表count- 在str中存儲字母的數量strlen(str) - count- 給出str中非字母的數量
相關用法
- C++ is_unsigned用法及代碼示例
- C++ is_fundamental用法及代碼示例
- C++ isdigit()用法及代碼示例
- C++ iswdigit()用法及代碼示例
- C++ is_scalar用法及代碼示例
- C++ is_pointer用法及代碼示例
- C++ is_polymorphic用法及代碼示例
- C++ is_rvalue_reference用法及代碼示例
- C++ is_class用法及代碼示例
- C++ iswxdigit()用法及代碼示例
- C++ isfinite()用法及代碼示例
- C++ isnormal()用法及代碼示例
- C++ isless()用法及代碼示例
- C++ ispunct()用法及代碼示例
- C++ iswupper()用法及代碼示例
- C++ is_trivial用法及代碼示例
- C++ is_void用法及代碼示例
- C++ isxdigit()用法及代碼示例
- C++ islessequal()用法及代碼示例
- C++ isunordered()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ isalpha()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
