在C++中,可以使用標(biāo)準(zhǔn)庫(kù)中的<cctype>
頭文件中的函數(shù)來(lái)實(shí)現(xiàn)大小寫字母的轉(zhuǎn)換。
toupper()
,例如:#include <iostream>
#include <cctype>
int main() {
char lowercase = 'a';
char uppercase = toupper(lowercase);
std::cout << uppercase << std::endl; // 輸出 'A'
return 0;
}
tolower()
,例如:#include <iostream>
#include <cctype>
int main() {
char uppercase = 'A';
char lowercase = tolower(uppercase);
std::cout << lowercase << std::endl; // 輸出 'a'
return 0;
}
需要注意的是,toupper()
和tolower()
函數(shù)接受一個(gè)整數(shù)類型的參數(shù),參數(shù)值必須是一個(gè)無(wú)符號(hào)字符或EOF。此外,如果參數(shù)不是一個(gè)小寫字母或大寫字母,這些函數(shù)不會(huì)進(jìn)行轉(zhuǎn)換,而是原樣返回該字符。