C 库 - towlower() 函数
C wctype 库中的 towlower() 函数用于将给定的宽字符转换为小写(如果可能)。
此函数可用于不区分大小写的比较、文本规范化或用户输入处理。
语法
以下是 towlower() 函数的 C 库语法 -
wint_t towlower( wint_t wc );
参数
此函数接受单个参数 -
-
wc - 它是一个需要转换为小写字母的"wint_t"类型的宽字符。
返回值
如果宽字符已转换为小写字母,则此函数返回小写字母,否则返回不变的字符(如果宽字符已经是小写字母或不是字母)。
示例 1
以下是演示 towlower() 函数用法的基本 C 语言示例。
#include <wchar.h>
#include <wctype.h>
#include <stdio.h>
int main() {
// 宽字符
wchar_t wc = L'G';
// 转换为小写
wint_t lower_wc = towlower(wc);
wprintf(L"The lowercase equivalent of %lc is %lc", wc, lower_wc);
return 0;
}
输出
以下是输出 -
The lowercase equivalent of G is g
示例 2
我们创建一个 C 程序来比较字符串是否相等。使用 towlower() 将其转换为小写后,如果两个字符串相等,则表示不区分大小写,否则区分大小写。
#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";
// 标志值
int equal = 1;
// 比较后,对两个字符串进行比较
for (size_t i = 0; i < wcslen(str1); ++i) {
if (towlower(str1[i]) != towlower(str2[i])) {
// 字符串不相等
equal = 0;
break;
}
}
if (equal) {
wprintf(L"The strings are equal (case insensitive).");
} else {
wprintf(L"The strings are not equal.");
}
return 0;
}
输出
以下是输出 -
The strings are equal (case insensitive).
示例 3
以下示例将宽字符规范化为小写字符。
#include <wchar.h>
#include <wctype.h>
#include <stdio.h>
int main() {
// 宽字符串
wchar_t text[] = L"Normalize THIS Tutorialspoint!";
size_t length = wcslen(text);
wprintf(L"Original text: %ls", text);
// 将文本标准化为小写
for (size_t i = 0; i < length; i++) {
text[i] = towlower(text[i]);
}
wprintf(L"Normalized text: %ls", text);
return 0;
}
输出
以下是输出 -
Original text: Normalize THIS Tutorialspoint! Normalized text: normalize this tutorialspoint!

