c++大小寫字母轉(zhuǎn)換的方法是什么

c++
小億
255
2023-12-08 00:19:40

在C++中,可以使用標(biāo)準(zhǔn)庫(kù)中的<cctype>頭文件中的函數(shù)來(lái)實(shí)現(xiàn)大小寫字母的轉(zhuǎn)換。

  1. 將字符轉(zhuǎn)換為大寫字母可以使用函數(shù)toupper(),例如:
#include <iostream>
#include <cctype>

int main() {
    char lowercase = 'a';
    char uppercase = toupper(lowercase);
    std::cout << uppercase << std::endl;  // 輸出 'A'
    return 0;
}
  1. 將字符轉(zhuǎn)換為小寫字母可以使用函數(shù)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)換,而是原樣返回該字符。

0