日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

字符串大小写字母转换c 语言,towlower()

發布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 字符串大小写字母转换c 语言,towlower() 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

wint_t towlower ( wint_t wc );

towlower() 函數用來將大寫字母轉換為小寫字母(針對寬字符)。

只有當參數 wc 是一個大寫字母,并且存在對應的小寫字母時,這種轉換才會發生。

towlower() 是 tolower() 的寬字符版本。

參數

wc

要被轉換的寬字符。它可以是一個有效的寬字符(被轉換為 wint_t?類型),也可以是 WEOF(表示無效的寬字符)。

返回值

如果轉換成功,那么返回與 wc 對應的小寫字母;如果轉換失敗,那么直接返回 wc(值未變)。

注意,返回值為 wint_t 類型,你可能需要隱式或者顯式地將它轉換為 wchar_t 類型。

實例

將一個寬字符串中的大寫字母轉換為小寫字母。

#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;

}

運行結果:

c c++ java python golang

關于大寫字母和小寫字母

人們通常認為只有"acb...xyz"才是小寫字母,只有"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

小寫字母包括:

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() 返回“真”的字符。

對于 towlower(),僅當有且只有一個對應的小寫字母時,這種轉換才能成功;如果沒有對應的小寫字母,或者有多個對應的小寫字母,那么轉換失敗。轉換成功返回對應的小寫字母,轉換失敗直接返回 wc(值未變)。

【實例】在簡體中文環境下檢測大小寫字母,并進行轉換。

#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

總結

以上是生活随笔為你收集整理的字符串大小写字母转换c 语言,towlower()的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。