比較簡單的一道題,主要是對字符串的一些基本的操作,這裏用到了string.h裏的strtok函數來對字符串進行分割,然後用STL的set存儲每個單詞,最後set的大小即爲不同的單詞的個數。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<set>
int main()
{
char buf[1024];
std::set<std::string> words;
while(gets(buf) != NULL)
{
if(buf[0] == '#')
break;
words.clear();
char *tmp = strtok(buf," ");
while(tmp != NULL)
{
words.insert(tmp);
tmp = strtok(NULL," ");
}
printf("%d\n",words.size());
}
return 0;
}