对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。在字符串中,单词之间通过空白符分隔,空白符包括:空格(' ')、制表符('\t')、回车符('\r')、换行符(' 用c++来写;样例输入 复制 if so, you already have a google account. you can sign in on the right. 样例输出 复制 If So, You Already Have A Google Account. You Can Sign In On The Right.
时间: 2024-04-10 15:33:35 浏览: 27
你可以使用以下代码来实现这个功能:
```cpp
#include <iostream>
#include <string>
#include <cctype>
std::string capitalizeWords(const std::string& input) {
std::string result = input;
bool capitalize = true;
for (size_t i = 0; i < result.length(); i++) {
if (std::isspace(result[i])) {
capitalize = true;
} else if (capitalize && std::islower(result[i])) {
result[i] = std::toupper(result[i]);
capitalize = false;
}
}
return result;
}
int main() {
std::string sentence = "if so, you already have a google account. you can sign in on the right.";
std::string capitalizedSentence = capitalizeWords(sentence);
std::cout << capitalizedSentence << std::endl;
return 0;
}
```
这段代码会将输入的句子中每个单词的首字母变成大写,并输出结果。
输出结果为:
```
If So, You Already Have A Google Account. You Can Sign In On The Right.
```
使用这个函数,你可以将输入的字符串中的所有单词的首字母变成大写字母。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)