字符串中不重复的单词个数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaocxks/article/details/72824892

输入:一个字符串,字符串由小写字母和空格组成,没有标点符号

输出:输入字符串中不重复的单词的个数

例如:

输入"i love you",输出3

输入"a a a",输出1

如果输入"a    aa",输出2

分隔符是空格,遇到空格代表一个单词结束,然后将单词插入到set集中,且都是不同的单词

但是当只有一个空格时,并且单词长度为0,就不用插入到set集中

#include<iostream>
using namespace std;
void numOfWords(char *str)
{
	int L=strlen(str);
	int i=0;
	string words="";
	set<string> cnt;
	whilt(i<L)
	{
	if(str[i]>='a'&&str[i]<='z')
	{
	words.push_back(str[i]);
	}
	if(str[i]==' '||i=L-1)
	{while(words.size()!=0)
	cnt.insert(words);
	words.clear();
	}
	i++;
	}
	cout<<cnt.size()<<endl;
	}
	int main()
	{
	char str[];
	get(str);
	numOfWords(str);
	}


扫描二维码关注公众号,回复: 5957151 查看本文章


猜你喜欢

转载自blog.csdn.net/xiaocxks/article/details/72824892