C 库 - iswupper() 函数
C wctype 库中的 iswupper() 函数用于检查给定的宽字符(用 wint_t 表示)是否为大写字母,例如"ABCDEFGHIJKLMNOPQRSTUVWXYZ"之一或当前语言环境特定的任何大写字母。
此函数可用于字符验证、大小写转换、字符串处理或标记化和解析。
语法
以下是 iswupper() 函数的 C 库语法 -
int iswupper( wint_t ch )
参数
此函数接受单个参数 -
-
ch - 需要检查的是"wint_t"类型的宽字符。
返回值
如果宽字符是大写字母,此函数返回非零值,否则返回零。
示例 1
以下是演示 iswupper() 函数用法的基本 C 语言示例。
#include <stdio.h> #include <wctype.h> #include <wchar.h> int main() { wchar_t wc = L'A'; if (iswupper(wc)) { wprintf(L"The character '%lc' is a uppercase letter. ", wc); } else { wprintf(L"The character '%lc' is not a uppercase letter. ", wc); } return 0; }
输出
以下是输出 -
The character 'A' is a uppercase letter.
示例 2
我们创建一个 C 程序,并使用 iswupper() 函数来计算给定宽字符中大写字母的数量。
#include <stdio.h> #include <wctype.h> #include <wchar.h> int main() { // 定义一个包含混合字符的宽字符串 wchar_t str[] = L"Hello, tutorialspoint India"; int uppercaseCount = 0; // 遍历宽字符串中的每个字符 for (int i = 0; str[i] != L'\0'; i++) { if (iswupper(str[i])) { uppercaseCount++; } } // 打印结果 wprintf(L"The wide string \"%ls\" contains %d uppercase letter(s). ", str, uppercaseCount); return 0; }
输出
以下是输出 -
The wide string "Hello, tutorialspoint India" contains 2 uppercase letter(s).
示例 3
下面是另一个示例,当我们在给定的宽字符中获取大写字母时,我们将打印大写字母。
#include <stdio.h> #include <wctype.h> #include <wchar.h> int main() { wchar_t str[] = L"Hello, tutorialspoint India"; // 遍历宽字符串中的每个字符 for (int i = 0; str[i] != L'\0'; i++) { if (iswupper(str[i])) { wprintf(L"%lc", str[i]); } } return 0; }
输出
以下是输出 -
HI