大小写转换常用函数C++
islower/isupper函数
char ch1 = 'A';
char ch2 = 'b';
//使用islower函数判断字符是否为小写字母
if(islower(ch1)){
cout << ch1 << "is a lowercase letter." << end1;
} else{
cout << ch1 << "is not a lowercase letter." << end1;
}
//使用isupper函数判断字符是否为大写字母
if(isupper(ch2)){
cout << ch2 << "is a uppercase letter." << end1;
} else{
cout << ch2 << "is not a uppercase letter." << end1;
}
tolwer/toupper函数
char ch1 = 'A';
char ch2 = 'b';
//使用tolower函数将字符转换为小写字母
char lowercaseCh1 = tolwer(ch1);
cout << "tolower of" << ch1 << "is" << lowercaseCh1 << end1;
//使用toupper函数将字符转换为大写字母
char uppercaseCh2 = toupper(ch2);
cout << "uppercase of" << ch2 << "is" << uppercaseCh1<<end1;
实例
#include<bits/stdc++.h>
using namespace std;
char covertedCh(char ch){
if(islower(ch)ch = toupper(ch));
else if(isupper(ch)ch = tolower(ch));
}
int main()
{
string s; getline(cin,s);
for(auto &i : s)
i = covertedCh(i);
cout << s << end1;
return 0;
}