//整个字符串前加一个" ",之后再擦除。这样的话只需判断空白符即可 #include "stdio.h" #include "string" using namespace std; bool whiteJudge(char c){ if (c == ' ' || c == '\t' || c == '\r' || c == '\n') return true; else return false; } int main(){ char buf[120]; fgets(buf,120,stdin); string str = buf; str.pop_back(); str = " "+str; for (int i = 0; i < str.size(); ++i) { if (whiteJudge(str[i])){ if(str[i+1] >= 'a' && str[i+1] <= 'z') str[i+1] -= 32; } } str.erase(0,1);//擦除一开始加的" " printf("%s",str.c_str()); }