字符串大小写转换
方法一:使用transform函数
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string str1="hersheyisunique!";
string str2="ABCDEFGHIJKLMNO!";
transform(str1.begin(),str1.end(),str1.begin(),::toupper);
transform(str2.begin(),str2.end(),str2.begin(),::tolower);
cout<<str1<<endl;
cout<<str2<<endl;
return 0;
}
输出:
HERSHEYISUNIQUE!
abcdefghijklmno!
方法二:直接+/-32
#include <iostream>
#include <string.h>
using namespace std;
int main() {
int t;
cin>>t;
string s[t];
cin.ignore();//用于忽略输入t时产生的换行符,不然输不进去字符串
for(int i=0; i<t; i++) {
getline(cin,s[i]);
}
for(int i=0; i<t; i++) {
string str=s[i];
char a=str[0]-32;//如果直接输出cout<<str[0]-32;结果为数字
cout<<a;
for(int j=0; j<str.length(); j++) {
if(str[j]==' ') {
char b=str[j+1]-32;
cout<<b;
}
}
cout<<endl;
}
return 0;
}
输入:
1
end of life
输出:
EOF