C 库 - iswalpha() 函数

❮ C 标准库 - <wctype.h>


C wctype 库中的 iswalpha() 函数用于检查给定的宽字符(用 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.", ch);
   } else {
      printf("The wide character %lc is not alphabetic.", 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";

   // 遍历宽字符串中的每个字符
   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;
    
    // 遍历宽字符串中的每个字符
    for (int i = 0; wc[i] != L'\0'; i++) {
      if (iswalpha(wc[i])) {
         alphabetic[index++] = wc[i];
      }
    }
    // 以空字符结尾的字符串
    alphabetic[index] = L'\0';
    
    // 打印字母字符
    wprintf(L"The alphabetic characters in the wide string \"%ls\" 
 are \"%ls\".", wc, alphabetic);
   
   return 0;
}

输出

以下是输出 -

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

❮ C 标准库 - <wctype.h>