#include
#include
#include
using namespace std;
int main()
{
string s("some string");
for (decltype(s.size()) index = 0; index != s.size() && !isspace(s[index]); ++index);
{
s[index] = toupper(s[index]);
}
cout << s << endl;
system("pause");
return 0;
}
你在main方法里定义一个index变量
这是动态语言写多了,刚跳过来的吧,s是不能修改的
先转成字符型数组,直接判断第一个字符是否为字母,是字母的话ascll码减26,,再将字符数组转为字符串.
试试这个代码
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s("some string");
transform(s.begin(),s.end(),s.begin(),toupper);
cout << s << endl;
system("pause");
return 0;
}
那再试试这个吧
int main()
{
string s("some string");
int point=s.find_first_of(" ");//寻找第一个空格键的位置
transform(s.begin(),s.begin()+point,s.begin(),toupper);
cout << s << endl;
system("pause");
return 0;
}