C 庫 - wctype() 函式



C 的wctypewctype()函式用於獲取寬字元型別的描述符(desc)。此描述符可與“iswctype”函式一起使用,以檢查給定的寬字元是否屬於特定的字元類別。

此函式可以儲存以下“str”值:

  • alnum - 識別 iswalnum 使用的類別
  • alpha - 識別 iswalpha 使用的類別
  • blank - 識別 iswblank (C99) 使用的類別
  • cntrl - 識別 iswcntrl 使用的類別
  • digit - 識別 iswdigit 使用的類別
  • graph - 識別 iswgraph 使用的類別
  • lower - 識別 iswlower 使用的類別
  • print - 識別 iswprint 使用的類別
  • space - 識別 iswspace 使用的類別
  • upper - 識別 iswupper 使用的類別
  • xdigit - 識別 iswxdigit 使用的類別

語法

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

wctype_t wctype( const char* str );

引數

此函式接受一個引數:

  • str - 這是一個指向型別為“const char*”的字串的指標,它指定要獲取其描述符的字元類別的名稱。

返回值

此函式返回型別為 wctype_t 的值,它是指定字元類別的描述符。

示例 1

以下是用wctype()獲取描述符的基本 C 程式示例。

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

int main() {
   // Wide character to check
   wchar_t wc = L'A';
   
   // Descriptor for alphabetic characters
   wctype_t desc = wctype("alpha");
   
   // Use iswctype to check if the character has the property described by desc
   if (iswctype(wc, desc)) {
      wprintf(L"%lc is an alphabetic character.\n", wc);
   } else {
      wprintf(L"%lc is not an alphabetic character.\n", wc);
   }
   
   return 0;
}

輸出

以下是輸出:

A is an alphabetic character.

示例 2

讓我們再建立一個 C 示例,並使用wctype()建立字母數字字元的描述符(desc)。

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

int main() {
   // Wide character string to check
   wchar_t str[] = L"tutorialspoint12";
   // Descriptor for alphanumeric characters
   wctype_t desc = wctype("alnum");

   // Loop through each character in the string
   for (size_t i = 0; str[i] != L'\0'; ++i) {
      // Check if the character is alphanumeric
      if (iswctype(str[i], desc)) {
         wprintf(L"%lc is an alphanumeric character.\n", str[i]);
      } else {
         wprintf(L"%lc is not an alphanumeric character.\n", str[i]);
      }
   }
   return 0;
}

輸出

以下是輸出:

t is an alphanumeric character.
u is an alphanumeric character.
t is an alphanumeric character.
o is an alphanumeric character.
r is an alphanumeric character.
i is an alphanumeric character.
a is an alphanumeric character.
l is an alphanumeric character.
s is an alphanumeric character.
p is an alphanumeric character.
o is an alphanumeric character.
i is an alphanumeric character.
n is an alphanumeric character.
t is an alphanumeric character.
1 is an alphanumeric character.
2 is an alphanumeric character.

示例 3

以下示例建立標點字元的描述符(desc)。然後我們檢查 wc 是否為標點字元。

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

int main() 
{ 
   wchar_t wc = L'#';
   // checks if the character is a punctuation
   if (iswctype(wc, wctype("punct"))) 
      wprintf(L"%lc is a punctuation\n", wc); 
   else
      wprintf(L"%lc is not a punctuation\n", wc);
   return 0; 
}

輸出

以下是輸出:

# is a punctuation
c_library_wctype_h.htm
廣告

© . All rights reserved.