C 庫 - towupper() 函式



C 的wctypetowupper() 函式用於將給定的寬字元轉換為大寫字元(如果可能)。

此函式可用於不區分大小寫的比較、文字規範化或使用者輸入處理。

語法

以下是 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
廣告

© . All rights reserved.