如何写一个能把一个句子中的每一个单词的首字母变成大写的程序?
例如input是hello world
output会是Hello World
用的C++
代码开头为
#include
#include
using namespace std;
int main() {
string line;
while (getline(cin,line)){
scout << line << ends;
}
}
如何写一个能把一个句子中的每一个单词的首字母变成大写的程序?
例如input是hello world
output会是Hello World
用的C++
代码开头为
#include
#include
using namespace std;
int main() {
string line;
while (getline(cin,line)){
scout << line << ends;
}
}
你题目的解答代码如下:
#include <iostream>
#include <string>
using namespace std;
int main() {
string line;
while (getline(cin,line)){
int len = line.length();
for (int i = 0; i < len; i++)
{
if (line[i]>='a' && line[i]<='z' && (i==0 || !(line[i-1]>='a' && line[i-1]<='z' || line[i-1]>='A' && line[i-1]<='Z')))
line[i] -= 32;
}
cout << line << endl;
}
}
如有帮助,望采纳!谢谢!