- #include<iostream>
- #include<string.h>
- using namespace std;
- string convert(char *p);
- int main()
- {
- char s[64];
- cout<<"请输入一句话:";
- cin.getline(s,64,'\\n'); //不能用cin,cin遇到空格停止输入
- cout<<convert(s);
- }
- string convert(char *s)
- {
- int i;
- char *t=new char[strlen(s)];
- for(i=0; i<strlen(s); i++)
- {
- if(i==0) //首字母大写
- *(t+i)=toupper(s[i]);
- else
- *(t+i)=s[i];
- if(s[i]==' ')
- {
- *(t+i+1)=toupper(s[i+1]); //单词首字母大写
- i++;
- }
- }
- string s1=t;
- return s1;
- delete []t;
- }
- //该片段来自于http://www.codesnippet.cn/detail/2408201614979.html
来源: http://www.codesnippet.cn/detail/2408201614979.html