33,311
社区成员
发帖
与我相关
我的任务
分享
while (cnt < s.length() && !isspace(s[cnt]))
个人认为将cnt < s.length放在前面比放在后面要好。原因自己可以考虑一下;#include <iostream>
#include <string>
#include <cctype>
using std::cout;
using std::endl;
using std::cin;
using std::string;
int main()
{
string s;
getline(cin, s);
int cnt = 0;
while (!isspace(s[cnt]) && cnt < s.length())
{
s[cnt] = toupper(s[cnt]);
++cnt;
}
cout << s << endl;
return 0;
}
修正代码如上,请楼主自己对照找原因