使用algorithm头文件中的transform()函数
将大写转换为小写,大写转换为小写
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main(){
string s;
cin>>s;
transform(s.begin(),s.end(),s.begin(),::tolower);
cout<<"转换为小写: "<<s<<endl;
transform(s.begin(),s.end(),s.begin(),::toupper);
cout<<"转换为大写: "<<s<<endl;
return 0;
}
运行结果:
abcDEFgh
转换为小写: abcdefgh
转换为大写: ABCDEFGH
函数transform()当中存在四个函数:
- 源目标起始迭代器地址
- 源目标结束迭代器地址
- 输出迭代器地址
- 自定义函数符(一元函数)
值得注意的是它只会对字母进行转换,不会对非字母元素进行转换
cstring中的strlwr()和strupr()函数将字符数组转换为字符串,并进行大小写的转换。
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
int main(){
char a[100] = "AbcdEfgh909q()";
string s1 = strlwr(a);
string s2 = strupr(a);
cout<<s1<<endl;
cout<<s2<<endl;
return 0;
}
运行结果:
abcdefgh909q()
ABCDEFGH909Q()