transform(str.begin(),str.end(),str.begin(),::tolower) | 字符串转小写 |
transform(str.begin(),str.end(),str.begin(),::toupper); | 字符串转大写 |
tolower(c) | 字符转小写 |
toupper(c) | 字符转大写 |
注意:第一种transform可以直接将字符串改变(本地修改)
第二种tolower不会直接改变字符(新建地址)
下面给出代码及结果方便理解(A——65;a——97):
#include <bits/stdc++.h>
using namespace std;
int main(){
string s="PcvgyGFuhjn";
transform(s.begin(),s.end(),s.begin(),::toupper);
cout<<s<<endl;
s[0]=tolower(s[0]);
cout<<s[0]<<endl;
//非STL
for(int i=0;i<s.length();i++){
if(s[i]>='a'&&s[i]<='z')
s[i]-=32;
}
cout<<s<<endl;
s[0]+=32;
cout<<s[0]<<endl;
return 0;
}