字符串大小写字母转换c 语言,towlower()
wint_t towlower ( wint_t wc );
towlower() 函數(shù)用來將大寫字母轉(zhuǎn)換為小寫字母(針對寬字符)。
只有當(dāng)參數(shù) wc 是一個大寫字母,并且存在對應(yīng)的小寫字母時,這種轉(zhuǎn)換才會發(fā)生。
towlower() 是 tolower() 的寬字符版本。
參數(shù)
wc
要被轉(zhuǎn)換的寬字符。它可以是一個有效的寬字符(被轉(zhuǎn)換為 wint_t?類型),也可以是 WEOF(表示無效的寬字符)。
返回值
如果轉(zhuǎn)換成功,那么返回與 wc 對應(yīng)的小寫字母;如果轉(zhuǎn)換失敗,那么直接返回 wc(值未變)。
注意,返回值為 wint_t 類型,你可能需要隱式或者顯式地將它轉(zhuǎn)換為 wchar_t 類型。
實(shí)例
將一個寬字符串中的大寫字母轉(zhuǎn)換為小寫字母。
#include
#include
#include
int main ()
{
int i=0;
wchar_t str[] = L"C C++ Java Python GoLang\n";
wchar_t c;
while (str[i])
{
c = str[i];
putwchar (towlower(c));
i++;
}
return 0;
}
運(yùn)行結(jié)果:
c c++ java python golang
關(guān)于大寫字母和小寫字母
人們通常認(rèn)為只有"acb...xyz"才是小寫字母,只有"ABC...XYZ"才是大寫字母,其實(shí)這是不對的。大小寫字母并不是固定的,不同的語言文化可能會包含不同的大小寫字母,例如在“簡體中文”環(huán)境中,西里爾文БГЁ、希臘文ΣΩΔΨΦ(數(shù)學(xué)物理公式中常用希臘字母)等都將成為大寫字母,它們對應(yīng)的小寫字母是бгё - σωδψφ。
我們可以通過 setlocale() 函數(shù)改變程序的地域設(shè)置,讓程序使用不同的字符集,從而支持不同的語言文化。
在默認(rèn)的地域設(shè)置(默認(rèn)為"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
在其它地域設(shè)置中,可能會使用 GBK(簡體中文)、BIG5(繁體中文)、Shift-JIS(日文)、Unicode(世界統(tǒng)一碼) 等更加復(fù)雜的編碼,它們包含了更多的大小寫字母。
也就是說,一個寬字符是否是大寫字母或者小寫字母和程序的地域設(shè)置有關(guān),不同的地域設(shè)置會包含不同的大小寫字母。
字母一定要區(qū)分大小寫嗎?
我們通常認(rèn)為,一個字母要么是小寫字母,要么是大寫字母;并且一個小寫字母必定對應(yīng)一個大寫字母,反之亦然。這種說法雖然適用于默認(rèn)的地域設(shè)置(默認(rèn)為"C"),但是并不一定適用于其它的地域設(shè)置。
以 Windows 下的“簡體中文”環(huán)境為例,拼音āōūǖ都將成為小寫字母,但是它們沒有對應(yīng)的大寫字母。Windows 下的“簡體中文”環(huán)境使用 GBK 編碼,該編碼并沒有包含āōūǖ這些大寫形式。
羅馬數(shù)字ⅲⅵⅶⅸ和ⅢⅥⅦⅨ也會被視為字母,并且從視覺上看起來是大小寫對應(yīng)的。其實(shí)不然,對于 Windows 來說,ⅲⅵⅶⅸ和ⅢⅥⅦⅨ都僅僅是字母而已,并沒有所謂的“大小寫”形式;換句話說,它們既不是大寫字母,也不是小寫字母,僅僅是字母而已。
以上說法僅適用于 Windows,在 Linux 和 Mac OS 下使用“簡體中文”情況會有所不同:
āōūǖ會有對應(yīng)的大寫字母āōūǖ,因?yàn)?Linux 和 Mac OS 下的“簡體中文”使用 Unicode 字符集(嚴(yán)格來說是 UTF-8 編碼),該字符集包含了世界上所有的字符。
在 Mac OS 下,ⅲⅵⅶⅸ和ⅢⅥⅦⅨ根本不會被視為字母;在 Linux 下,ⅲⅵⅶⅸ被視為小寫字母,ⅢⅥⅦⅨ被視為大寫字母。
站在專業(yè)角度看問題
C語言標(biāo)準(zhǔn)規(guī)定,在默認(rèn)的"C"地域設(shè)置中,只有 iswlower() 或者 iswupper() 返回“真”的字母才會被視為字母;也就是說,一個字母要么是小寫字母,要么是大寫字母。
但是對于其它的地域設(shè)置,C語言并沒有這種規(guī)定,一個字母可以是 iswlower() 或者 iswupper() 返回“真”的字符,也可以是被當(dāng)前語言環(huán)境顯式地(刻意地)指定為字母的字符,例如羅馬數(shù)字ⅲⅵⅶⅸ和ⅢⅥⅦⅨ。但是有一個原則,被指定為字母的字符一定不能是 iswcntrl()、iswdigit()、iswpunct()、iswspace() 返回“真”的字符。
對于 towlower(),僅當(dāng)有且只有一個對應(yīng)的小寫字母時,這種轉(zhuǎn)換才能成功;如果沒有對應(yīng)的小寫字母,或者有多個對應(yīng)的小寫字母,那么轉(zhuǎn)換失敗。轉(zhuǎn)換成功返回對應(yīng)的小寫字母,轉(zhuǎn)換失敗直接返回 wc(值未變)。
【實(shí)例】在簡體中文環(huán)境下檢測大小寫字母,并進(jìn)行轉(zhuǎn)換。
#include
#include
#include
int main ()
{
int i = 0;
wchar_t str[] = L"σωδБГЁāōūⅢⅥⅨⅲⅵⅸ";
wchar_t c;
setlocale(LC_ALL, "zh_CN.UTF-8"); //設(shè)置為簡體中文,使用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 下的運(yùn)行結(jié)果:
σ 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 下的運(yùn)行結(jié)果:
σ 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 下的運(yùn)行結(jié)果:
σ 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
總結(jié)
以上是生活随笔為你收集整理的字符串大小写字母转换c 语言,towlower()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 项目复盘思考
- 下一篇: JAVA性能分析工具--Jvisualv