您当前的位置:首页 > 计算机 > 编程开发 > VC/VC++

iswupper()_C语言iswupper()详解:判断一个宽字符是否是大写字母

时间:12-25来源:作者:点击数:

int iswupper (wint_t wc);

iswupper() 函数用来检测一个宽字符是否是大写字母。

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

参数

  • wc

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

返回值

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

实例

判断字符串中的字符是否是大写字母,如果是,那么将它转换为小写字母。

#include <stdio.h>
#include <wctype.h>
int main ()
{
    int i=0;
    wchar_t str[] = L"C C++ Java Python GoLang\n";
    wchar_t c;
    while (str[i])
    {
        c = str[i];
        if (iswupper(c)) c = towlower(c);
        putwchar (c);
        i++;
    }
    return 0;
}

运行结果:

c c++ java python golang

关于大写字母

人们通常认为只有"ABC...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

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

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

但是有一个原则,被视为大写字母的宽字符一定不能是 iswcntrl()、iswdigit()、iswpunct()、iswspace() 返回“真”的字符。

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

#include <wctype.h>
#include <wchar.h>
#include <locale.h>
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 wprintf(L"%lc is not upper\n", c);
        i++;
    }
    return 0;
}

运行结果:

Б is upper, the lower is б
Г is upper, the lower is г
Ё is upper, the lower is ё
- is not upper
Σ is upper, the lower is σ
Ω is upper, the lower is ω
Δ is upper, the lower is δ
Ψ is upper, the lower is ψ
Φ is upper, the lower is φ
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门