Here is one way to do it, depending on how you define a "word". If all you want to use is stdin, then just replace FILE* fp = fopen("\\tmp\\constitution.txt","r"); with FILE* fp = stdin;
Code:
#include <stdio.h>
#include <string.h>
#define SZARRAY(x) sizeof(x)/sizeof(x[0])
int find_array(const char*word, const char**word_array,int nItems)
{
for(int i = 0; i < nItems; i++)
{
if(strcmp(word_array[i],word) == 0)
return 1;
}
return 0;
}
int main(int argc, char* argv[])
{
char* word_array[1000] = {0}; // room for 1000 strings
char word[1024];
int word_counter = 0;
FILE* fp = fopen("\\tmp\\constitution.txt","r");
if(fp == 0)
{
printf("file cannot be opened\n");
return 1;
}
while( fscanf(fp,"%s",word) > 0 && word_counter < SZARRAY(word_array))
{
if( !find_array(word,(const char**)word_array,word_counter))
{
word_array[word_counter] = strdup(word);
++word_counter;
}
}
fclose(fp);
printf("unique words = %d\n",word_counter);
return 0;
}