#include <string.h>
#include <stdio.h>
#define MAX_WORDS 100

void insertUW(char *string);
int searchUW(char *string);
void printUW(void);

int uwc=0;     //Keeps The Number Of Unique Words Already found.(Unique Word Counter)

struct uwss   //Unique Word Status Structure
  {        
   char uw[25];
   int no;
  }uws[MAX_WORDS];

int main(int argc,char *argv[])
  {
     FILE *in;
     if(argc > 1)
         {
            if((in = fopen(argv[1],"r"))==NULL)
               { 
                  printf("The file  \"%s\" not found.\n",argv[1]); 
                  return 0;
               }
         }
     else
        {
          printf("    Pleas enter a file name.\n");
          printf("    Usage: extract filename\n");
          return 0;
        }
     char string[100]; //Assume the maximum length of a line 100 characters
     const char delimiters[] = " .,\n"; //word delimiters
     char *token;

     while(fgets(string,100,in)!=NULL)
       {
          token = strtok (string, delimiters);  
      
          while(token!=NULL)
            {
               insertUW(token);
               token = strtok (NULL, delimiters);    

            }//end of while
       }//end of while
 
     printUW();
     return 0 ;
  }

void insertUW(char *string)
  {
     int index=searchUW(string);

     strncpy(uws[index].uw,string,25);
     uws[index].no++;
     if(index==uwc)
     uwc++;
  }

int searchUW(char *string)
  {
     int i;

     if(uwc!=0)
        for(i=0;i<uwc;i++)
           if((strncmp(uws[i].uw,string,25))==0)
              return i;//end of if-for-if

     return uwc;
  }

void printUW(void)
  {
     int i,j;

     for(i=0;i<uwc;i++)
        {
           printf("------------------------------------\n");
           printf("%d) %s     ",i+1,uws[i].uw);

           for(j=0;j<(26-strlen(uws[i].uw));j++)
              printf(" ");//end of for

           printf("(%d)\n",uws[i].no);
        }//end of for
  }
