C 庫 - iswalpha() 函式



C 的wctypeiswalpha() 函式用於檢查給定的寬字元(由 wint_t 表示)是否為字母字元,即大寫字母、小寫字母或當前區域設定特有的任何字母字元。

此函式可用於字元驗證、大小寫轉換、密碼驗證、字串處理或標記化和解析。

語法

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

int iswalpha( wint_t ch )

引數

此函式接受一個引數:

  • ch - 它是要檢查的型別為 'wint_t' 的寬字元。

返回值

如果寬字元是字母字元,則此函式返回非零值,否則返回零。

示例 1

以下是演示 iswalpha() 函式用法的基本 C 示例。

#include <wctype.h>
#include <stdio.h>
int main() {
   wint_t ch = L'T'; 

   if (iswalpha(ch)) {
      printf("The wide character %lc is alphabetic.\n", ch);
   } else {
      printf("The wide character %lc is not alphabetic.\n", ch);
   }
   return 0;
}

輸出

以下是輸出:

The wide character T is alphabetic.

示例 2

在這裡,我們使用 iswalpha() 函式列印字母字元。

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

int main() {
   wchar_t wc[] = L"tutorialspoint 500081";

   // Iterate over each character in the wide string
   for (int i = 0; wc[i] != L'\0'; i++) {
      if (iswalpha(wc[i])) {
         wprintf(L"%lc", wc[i]);
      }
   }
   return 0;
}

輸出

以下是輸出:

tutorialspoint

示例 3

讓我們建立一個另一個 C 程式來提取並列印寬字元中的字母字元。

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

int main() {
   wchar_t wc[] = L"2006 tutorialspoint India Pvt.Ltd 2024";
   wchar_t alphabetic[100];
   int index = 0;
   
   // Iterate over each character in the wide string
   for (int i = 0; wc[i] != L'\0'; i++) {
      if (iswalpha(wc[i])) {
         alphabetic[index++] = wc[i];
      }
   }
   // null terminated string
   alphabetic[index] = L'\0';
   
   // print the alphabetic character
   wprintf(L"The alphabetic characters in the wide string \"%ls\" \n are \"%ls\".\n", wc, alphabetic);
   
   return 0;
}

輸出

以下是輸出:

The alphabetic characters in the wide string "2006 tutorialspoint India Pvt.Ltd 2024" 
 are "tutorialspointIndiaPvtLtd".
c_library_wctype_h.htm
廣告

© . All rights reserved.