- 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库 - towctrans() 函数
C 的wctype库 towctrans() 函数用于根据指定的转换描述符转换宽字符。
“转换描述符”是一个定义特定字符转换类型的对象,例如将字符转换为大写或小写。这些描述符被诸如“towctrans”之类的函数用于对宽字符执行所需的转换。
语法
以下是 towctrans() 函数的C库语法:
wint_t towctrans( wint_t wc, wctrans_t desc );
参数
此函数接受一个参数:
-
wc − 它是需要转换的类型为'wint_t'的宽字符。
-
desc − 它是类型为wctrans_t的描述符。该描述符通常使用wctrans函数获得。
返回值
如果wc(宽字符)具有“desc”指定的属性,则此函数返回非零值;否则返回零。
示例1
以下示例演示了使用 towctrans() 将宽字符转换为其大写等效项。
#include <wctype.h>
#include <wchar.h>
#include <stdio.h>
int main() {
// The wide character to be transformed
wint_t wc = L'a';
// Get the transformation descriptor for uppercase conversion
wctrans_t to_upper = wctrans("toupper");
if (to_upper) {
wint_t res= towctrans(wc, to_upper);
if (res != WEOF) {
wprintf(L"Transformed character: %lc\n", res);
} else {
wprintf(L"Transformation failed.\n");
}
} else {
wprintf(L"Invalid transformation descriptor.\n");
}
return 0;
}
输出
以下是输出:
Transformed character: A
示例2
让我们创建另一个示例,我们使用 towctrans() 创建desc(描述符)来将小写字符转换为大写,反之亦然。
#include <wctype.h>
#include <wchar.h>
#include <stdio.h>
int main()
{
wchar_t str[] = L"Tutorialspoint India";
wprintf(L"Before transformation \n");
wprintf(L"%ls \n", str);
for (size_t i = 0; i < wcslen(str); i++) {
// check if it is lowercase
if (iswctype(str[i], wctype("lower"))) {
// transform character to uppercase
str[i] = towctrans(str[i], wctrans("toupper"));
}
// checks if it is uppercase
else if (iswctype(str[i], wctype("upper"))){
// transform character to uppercase
str[i] = towctrans(str[i], wctrans("tolower"));
}
}
wprintf(L"After transformation \n");
wprintf(L"%ls", str);
return 0;
}
输出
以下是输出:
Before transformation Tutorialspoint India After transformation tUTORIALSPOINT iNDIA
示例3
下面的示例使用 towctrans() 创建desc(描述符)来将大写字符转换为小写,反之亦然。
#include <wctype.h>
#include <wchar.h>
#include <stdio.h>
int main()
{
wchar_t str[] = L"Hello, Hyderabad visit Tutorialspoint 500081";
wprintf(L"Original string: %ls \n", str);
for (size_t i = 0; i < wcslen(str); i++) {
// If the character is lowercase, transform it to uppercase
if (iswctype(str[i], wctype("lower"))) {
str[i] = towctrans(str[i], wctrans("toupper"));
}
// If the character is uppercase, transform it to lowercase
else if (iswctype(str[i], wctype("upper"))) {
str[i] = towctrans(str[i], wctrans("tolower"));
}
}
wprintf(L"Transformed string: %ls \n", str);
return 0;
}
输出
以下是输出:
Original string: Hello, Hyderabad visit Tutorialspoint 500081 Transformed string: hELLO, hYDERABAD VISIT tUTORIALSPOINT 500081
c_library_wctype_h.htm
广告