C 庫 - towlower() 函式



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

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

語法

以下是 towlower() 函式的 C 庫語法:

wint_t towlower( wint_t wc );

引數

此函式接受單個引數:

  • wc − 一個型別為 'wint_t' 的寬字元,將被轉換為小寫。

返回值

如果寬字元已更改為小寫字元,則此函式返回小寫字元;否則返回未更改的字元(如果它已經是小寫字元或不是字母字元)。

示例 1

以下是一個基本的 C 示例,演示了 towlower() 函式的使用。

#include <wchar.h>
#include <wctype.h>
#include <stdio.h>

int main() {    
   // wide character
   wchar_t wc = L'G';
   // convert to lowercase
   wint_t lower_wc = towlower(wc);
   
   wprintf(L"The lowercase equivalent of %lc is %lc\n", 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";
   
   // flag value
   int equal = 1;

   // comepare both string after compare
   for (size_t i = 0; i < wcslen(str1); ++i) {
      if (towlower(str1[i]) != towlower(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 THIS Tutorialspoint!";
   size_t length = wcslen(text);

   wprintf(L"Original text: %ls\n", text);

   // Normalize the text to lowercase
   for (size_t i = 0; i < length; i++) {
      text[i] = towlower(text[i]);
   }
   wprintf(L"Normalized text: %ls\n", text);
   
   return 0;
}

輸出

以下是輸出:

Original text: Normalize THIS Tutorialspoint!
Normalized text: normalize this tutorialspoint!
c_library_wctype_h.htm
廣告
© . All rights reserved.