c语言判断m为英文字母,iswalpha()

int iswalpha (wint_t wc);

iswalpha() 用来检测一个宽字符是否是字母。

如果一个宽字符被 iswupper() 或者 iswlower() 检测后返回“真”,那么它一定会被 iswalpha() 判定为字母。

iswalpha() 是 isalpha()(位于中)的宽字符版本:如果参数 wc 能够被 wctob() 转换为单个字节的形式,并且转换后的字符使用 isalpha() 检测返回“真”(非零值),那么 iswalpha() 也一定会将 wc 判定为字母。

参数

wc

要检测的宽字符。它可以是一个有效的宽字符(被转换为 wint_t 类型),也可以是 WEOF(表示无效的宽字符)。

返回值

返回值为非零(真)表示wc是字母,返回值为零(假)表示wc不是字母。

实例

判断一个字符串中的字符是否是字母。

#include

#include

#include

int main ()

{

int i=0;

wchar_t str[] = L"C++ C# .NET";

while (str[i])

{

if (iswalpha(str[i])) wprintf (L"'%lc' is alphabetic\n", str[i]);

else wprintf (L"'%lc' is not alphabetic\n", str[i]);

i++;

}

return 0;

}

运行结果:

'C' is alphabetic

'+' is not alphabetic

'+' is not alphabetic

' ' is not alphabetic

'C' is alphabetic

'#' is not alphabetic

' ' is not alphabetic

'.' is not alphabetic

'N' is alphabetic

'E' is alphabetic

'T' is alphabetic

关于字母

人们通常认为只有"abc...xyzABC...XYZ"才是字母,其实这是不对的。字母并不是固定的,不同的语言文化可能会包含不同的字母,例如在“简体中文”环境中,西里尔文БГЁ、希腊文ΣΩΔΨΦ(数学物理公式中常用希腊字母)等都将成为字母。

БГЁ - ΣΩΔΨΦ 都是大写字母,它们对应的小写字母是:

бгё - σωδψφ

我们可以通过 setlocale() 函数改变程序的地域设置,让程序使用不同的字符集,从而支持不同的语言文化。

在默认的地域设置(默认为"C")中,C语言通常使用 ASCII 编码,能较好地支持英文,此时字母包括:

a b c d e f g h i j k l m n o p q r s t u v w x y z  A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

在其它地域设置中,可能会使用 GBK(简体中文)、BIG5(繁体中文)、Shift-JIS(日文)、Unicode(世界统一码) 等更加复杂的编码,它们包含了更多的字母。

也就是说,一个宽字符是否是字母和程序的地域设置有关,不同的地域设置会包含不同的字母。

字母一定要区分大小写吗?

我们通常认为,一个字母要么是小写字母,要么是大写字母;并且一个小写字母必定对应一个大写字母,反之亦然。这种说法虽然适用于默认的地域设置(默认为"C"),但是并不一定适用于其它的地域设置。

以 Windows 下的“简体中文”环境为例,拼音āōūǖ都将成为小写字母,但是它们没有对应的大写字母。Windows 下的“简体中文”环境使用 GBK 编码,该编码并没有包含ĀŌŪǕ这些大写形式。

罗马数字ⅲⅵⅶⅸ和ⅢⅥⅦⅨ也会被视为字母,并且从视觉上看起来是大小写对应的。其实不然,对于 Windows 来说,ⅲⅵⅶⅸ和ⅢⅥⅦⅨ都仅仅是字母而已,并没有所谓的“大小写”形式;换句话说,它们既不是大写字母,也不是小写字母,仅仅是字母而已。

以上说法仅适用于 Windows,在 Linux 和 Mac OS 下使用“简体中文”情况会有所不同:

āōūǖ会有对应的大写字母ĀŌŪǕ,因为 Linux 和 Mac OS 下的“简体中文”使用 Unicode 字符集(严格来说是 UTF-8 编码),该字符集包含了世界上所有的字符。

在 Mac OS 下,ⅲⅵⅶⅸ和ⅢⅥⅦⅨ根本不会被视为字母;在 Linux 下,ⅲⅵⅶⅸ被视为小写字母,ⅢⅥⅦⅨ被视为大写字母。

站在专业角度看问题

C语言标准规定,在默认的"C"地域设置中,只有 iswlower() 或者 iswupper() 返回“真”的字母才会被视为字母;也就是说,一个字母要么是小写字母,要么是大写字母。

但是对于其它的地域设置,C语言并没有这种规定,一个字母可以是 iswlower() 或者 iswupper() 返回“真”的字符,也可以是被当前语言环境显式地(刻意地)指定为字母的字符,例如罗马数字ⅲⅵⅶⅸ和ⅢⅥⅦⅨ。但是有一个原则,被指定为字母的字符一定不能是 iswcntrl()、iswdigit()、iswpunct()、iswspace() 返回“真”的字符。

【实例】在简体中文环境下检测字母。

#include

#include

#include

int main ()

{

int i = 0;

wchar_t str[] = L"σωδБГЁāōūⅢⅥⅨⅲⅵⅸ";

wchar_t c;

setlocale(LC_ALL, "zh_CN.UTF-8"); //设置为简体中文,使用UTF-8编码

//在 Windows 下可以写作 setlocale(LC_ALL, ""); 或者 setlocale(LC_ALL, "chs");

//在 Linux 下可以写作 setlocale(LC_ALL, ""); 或者 setlocale(LC_ALL, "zh_CN.UTF-8");

//在 Mac OS 下可以写作 setlocale(LC_ALL, "zh_CN"); 或者 setlocale(LC_ALL, "zh_CN.UTF-8");

while (str[i])

{

c = str[i];

if (iswupper(c)) wprintf(L"%lc is upper, the lower is %lc\n", c, towlower(c));

else if(iswlower(c)) wprintf(L"%lc is lower, the upper is %lc\n", c, towupper(c));

else if(iswalpha(c)) wprintf(L"%lc is alphabetic\n", c);

else wprintf(L"%lc is a character\n", c);

i++;

}

return 0;

}

在 Windows 下的运行结果:

σ is lower, the upper is Σ

ω is lower, the upper is Ω

δ is lower, the upper is Δ

Б is upper, the lower is б

Г is upper, the lower is г

Ё is upper, the lower is ё

is lower, the upper is ā

is lower, the upper is ō

is lower, the upper is ū

Ⅲ is alphabetic

Ⅵ is alphabetic

Ⅸ is alphabetic

ⅲ is alphabetic

ⅵ is alphabetic

ⅸ is alphabetic

在 Linux 下的运行结果:

σ is lower, the upper is Σ

ω is lower, the upper is Ω

δ is lower, the upper is Δ

Б is upper, the lower is б

Г is upper, the lower is г

Ё is upper, the lower is ё

ā is lower, the upper is Ā

ō is lower, the upper is Ō

ū is lower, the upper is Ū

Ⅲ is upper, the lower is ⅲ

Ⅵ is upper, the lower is ⅵ

Ⅸ is upper, the lower is ⅸ

ⅲ is lower, the upper is Ⅲ

ⅵ is lower, the upper is Ⅵ

ⅸ is lower, the upper is Ⅸ

在 Mac OS 下的运行结果:

σ is lower, the upper is Σ

ω is lower, the upper is Ω

δ is lower, the upper is Δ

Б is upper, the lower is б

Г is upper, the lower is г

Ё is upper, the lower is ё

ā is lower, the upper is Ā

ō is lower, the upper is Ō

ū is lower, the upper is Ū

Ⅲ is a character

Ⅵ is a character

Ⅸ is a character

ⅲ is a character

ⅵ is a character

ⅸ is a character

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值