字符串首字母置为大写

来源:互联网 发布:黑马程序员的简历 编辑:程序博客网 时间:2024/05/16 04:39

字符串首字母置为大写,比如hello you are my sunshine !

变为Hello You Are My Sunshine !

源代码如下:

#include <iostream>#include <cctype>#include <string>using namespace std;void first_letter(string &);int main(){string str = "hello you are my sunshine !";first_letter(str);cout<<"The output string is :"<<str<<endl;system("pause");return 0;}void first_letter(string & s){string::iterator it = s.begin();bool flag = true;while (it!=s.end()){if (isalpha(*it) && flag){*it = toupper(*it);flag = false;}if (isspace(*it)){flag = true;}it++;}}
结果如下:



0 0