大一
· 例1
输入一行句子,将每个单词的首字母变为大写后输出
将字符串分割为单词
- stringstream
string a;
getline(cin,a);
stringstream ss(a);//ss为stringstream对象
string temp;
while(ss>>temp)//>>表示将流中提取一个单词;<<表示将一个string对象输入到流中
{
if(temp[0]<='z'&&temp[0]>='a')
temp[0]=temp[0]-'a'+'A';
cout<<temp<<' ';
}
cout<<endl;
- strtok
char a[1100];
scanf("%[^\n]",a);
getchar();
//gets(a);
char *p;
p=strtok(a," ");
while(p!=NULL)
{
if(p[0]<='z'&&p[0]>='a')
p[0]=p[0]-'a'+'A';
cout<<p<<' ';
p=strtok(NULL," ");//参数为NULL,默认从上次暂停的位置继续切割
}
cout<<endl;