跳到正文

个人记录

大一

Yaha约 1 分钟
原文
文章信息与版本

文章信息

发布
2024年11月6日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 1 分钟
获赞
1

版本

  1. v1当前

· 例1

输入一行句子,将每个单词的首字母变为大写后输出

将字符串分割为单词

  1. stringstream
C++
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;
  1. strtok
C++
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;
原文