字符串-首字母变大写

题目描述
输入一行英文句子,将每个单词的第一个字母改成大写字母。
输入
一个长度不超过 100 的英文句子.
输出
将原先句子中单词的第一个字母改成大写字母输出.
输入输出样例
样例输入 #1
i want to get an accepted
样例输出 #1
I Want To Get An Accepted

#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
int main()
{
    for_each(istream_iterator<string>(cin), istream_iterator<string>(), [](auto str) { str[0] = toupper(str[0]); cout << str << ' '; });
}

供参考:

#include<stdio.h>
#include<ctype.h>
int main()
{
    char a[100+1], c;
    int i;
    gets(a);
    if (a[0] != ' ') {
        if (islower(a[0]))//判断s[0]是单词开始
            a[0] = toupper(a[0]);
    }
    for (i = 0; (c = a[i]) != '\0'; i++)
    {
        if (c == ' ' && a[i + 1] != ' ') {//判断s[i+1]是一个单词的开始
            if (islower(a[i + 1]))//判断是否是小写字符
                a[i + 1] = toupper(a[i + 1]);
        }
    }
    puts(a);

    return 0;
}

#include <iostream>
#include <string.h>
using namespace std;

int main() {
    string s;
    getline(cin, s);
    
    if(s[0] >= 'a' && s[0] <= 'z') s[0] -= 32;
    for(int i = 0; i < s.size(); i++) if(s[i] == ' ' && s[i + 1] >= 'a' && s[i + 1] <= 'z') s[i + 1] -= 32;
    
    cout << s;
    
    return 0;
}