- C 标准库
- C 库 - 首页
- C 库 - <assert.h>
- C 库 - <complex.h>
- C 库 - <ctype.h>
- C 库 - <errno.h>
- C 库 - <fenv.h>
- C 库 - <float.h>
- C 库 - <inttypes.h>
- C 库 - <iso646.h>
- C 库 - <limits.h>
- C 库 - <locale.h>
- C 库 - <math.h>
- C 库 - <setjmp.h>
- C 库 - <signal.h>
- C 库 - <stdalign.h>
- C 库 - <stdarg.h>
- C 库 - <stdbool.h>
- C 库 - <stddef.h>
- C 库 - <stdio.h>
- C 库 - <stdlib.h>
- C 库 - <string.h>
- C 库 - <tgmath.h>
- C 库 - <time.h>
- C 库 - <wctype.h>
- C 标准库资源
- C 库 - 快速指南
- C 库 - 有用资源
- C 库 - 讨论
C 库 - towupper() 函数
C 的wctype库 towupper() 函数用于将给定的宽字符转换为大写字符(如果可能)。
此函数可用于不区分大小写的比较、文本规范化或用户输入处理。
语法
以下是 towupper() 函数的 C 库语法:
wint_t towupper( wint_t wc );
参数
此函数接受一个参数:
-
wc - 它是类型为 'wint_t' 的宽字符,需要转换为大写。
返回值
如果宽字符已更改为大写字符,则此函数返回大写字符;否则返回未更改的字符(如果它已经是大写字符或不是字母字符)。
示例 1
以下是演示 towupper() 函数用法的基本 C 示例。
#include <wchar.h>
#include <wctype.h>
#include <stdio.h>
int main() {
// wide character
wchar_t wc = L'a';
// convert to upper
wint_t upper_wc = towupper(wc);
wprintf(L"The uppercase equivalent of %lc is %lc\n", wc, upper_wc);
return 0;
}
输出
以下是输出:
The uppercase equivalent of a is A
示例 2
我们创建一个 C 程序来检查字符串是否相等。使用 towupper() 转换为大写后。如果两个字符串相等,则表示不区分大小写,否则表示区分大小写。
#include <wchar.h>
#include <wctype.h>
#include <stdio.h>
#include <string.h>
int main() {
wchar_t str1[] = L"hello world";
wchar_t str2[] = L"HELLO WORLD";
// flag value
int equal = 1;
// comepare both string after compare
for (size_t i = 0; i < wcslen(str1); i++) {
if (towupper(str1[i]) != towupper(str2[i])) {
// strings are not equal
equal = 0;
break;
}
}
if (equal) {
wprintf(L"The strings are equal (case insensitive).\n");
} else {
wprintf(L"The strings are not equal.\n");
}
return 0;
}
输出
以下是输出:
The strings are equal (case insensitive).
示例 3
以下示例将宽字符规范化为大写字符。
#include <wchar.h>
#include <wctype.h>
#include <stdio.h>
int main() {
// Wide string
wchar_t text[] = L"Normalize Tutorialspoint!";
size_t length = wcslen(text);
wprintf(L"Original text: %ls\n", text);
// Normalize the text to uppercase
for (size_t i = 0; i < length; i++) {
text[i] = towupper(text[i]);
}
wprintf(L"Normalized text: %ls\n", text);
return 0;
}
输出
以下是输出:
Original text: Normalize Tutorialspoint! Normalized text: NORMALIZE TUTORIALSPOINT!
c_library_wctype_h.htm
广告