将一个句子中单词的首字母转换为大写

如:

     hello my name is zeroinger , nice to meet you!

转换后:

     Hello My Name Is Zeroinger , Nice To Meet You!

代码:

    

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <cctype>
using namespace std;
string slove(string& str)
{
    //string 指针指向字符串首地址
    string::iterator it =str.begin();
    //空格标志位
    bool flag_space=true;
    //循环遍历句子
    while(it!=str.end())
    {   //是否是一个单词的首字母
        if(isalpha(*it) && flag_space)
        {

            *it=toupper(*it);
          //  it++;
            flag_space = false;
        }
        //如果是空格,标志位置1
        if(isspace(*it))
        {
            flag_space=true;
        }
        it++;
    }
    return str;
}
int main()
{
    //字符串读入方式也该注意
     string str1,str2;
    // str1 = "hello my name is zeroinger , nice to meet you!";
     getline(cin,str1);
     str2=slove(str1);
     cout<<str2<<endl;
     return 0;
}

  

    

原文地址:https://www.cnblogs.com/Zeroinger/p/5572375.html