- 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 程式設計資源
- 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
廣告