title: Counting Words from Text File into Linked List in C
body: <p>I am new to programming in C and for an assignment for one of my classes, we are to write code that would read in a text file, put all the words in a linked list with a counter, and output that linked list into a text file.</p>
  
  <p>I'm not sure what I am doing wrong in my below code, it only counts some of the words and seems to skip others. (I added many extra prints to try and see where the words are lost)</p>
  
  <p>I then attempted to make all the words lowercase when they were added to the linked list, but that seemed to make it read even less words and I have no clue why.</p>
  
  <p>Any help/suggestions would be greatly appreciated.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;ctype.h&gt;
  
  typedef struct s_words {
      char *str; //word
      int count; //number of times word occurs
      struct s_words* next; //pointer to next word
  } words;
  
  
  words* create_words(char* word) {
      //+allocate space for the structure
      printf("%lu ", strlen(word));
      words* newWord = malloc(sizeof(words));
      if (NULL != newWord)
      {
          //+allocate space for storing the new word in "str"
          //+if str was array of fixed size, storage wud be wasted
          newWord-&gt;str = (char *)malloc((strlen(word))+1);
          strcpy(newWord-&gt;str, word); //+copy “word” into newWord-&gt;str
          newWord-&gt;str[strlen(word)]='\0';
          printf(" Create: %s ", newWord-&gt;str);
          //+initialize count to 1;
          newWord-&gt;count = 1;
          //+initialize next;
          newWord-&gt;next = NULL;                   
      }
      return newWord;
  }
  
  words* add_word(words* wordList, char* word) 
  {
      int found=0;
      words *temp=wordList;
      //+ search if word exists in the list; if so, make found=1
      while (temp != NULL) 
      {
  
          if (strcmp(temp-&gt;str, word) == 0) 
          {  //+use strcmp command
              found=1;
              temp-&gt;count = temp-&gt;count+1; //+increment count;
              return wordList;
          }
          else 
          {
              //+update temp
              temp = temp-&gt;next;
          }
      }
      if (found==0) 
      {  //new word
          //printf("%s ", word);
          words* newWord = create_words(word);
          if (NULL != newWord) 
          {
              //+??  Insert new word at the head of the list
              newWord-&gt;next = wordList;
              printf(" NEW WORD: %s\n ", newWord-&gt;str);
          }
          return newWord;
      }
      //return wordList; //code never gets here, just added in case of error    
  }
  
  
  
  
  int main(int argc, char* argv[]) 
  {
  
      words *mywords;  //+head of linked list containing words
      mywords=NULL;
  
      FILE *myFile;
      FILE *myOutput;
  
      char* filename = argv[1];
      char* outputfile = argv[2];
  
      myFile = fopen(filename, "r");  //+first parameter is input file
      if (myFile==0) 
      {
          printf("file not opened\n");
          return 1;
      }
      else 
      {
          printf("file opened \n");
      }
  
      //+start reading file character by character;
      //+when word has been detected; call the add_word function
  
      int ch = 0, word = 1, k = 0;
      char thisword[100];
      //ch = putchar(tolower(ch));
      //ch = fgetc(myFile);
      while ((ch = fgetc(myFile)) != EOF )
      {
          //error handling
  
          if (ch == '.' || ch == ' ' || ch == ',' || ch == ':' || ch == ';' || ch == '\n')  //+detect new word? Check if ch is a delimiter
          { //when above if is true, new word created in next if:
              if ( word == 1 )  //+make sure previous character was not delimiter
              {
                  word = 0;
                  //+make the kth character of thisword as \0
                  thisword[k] = '\0';
  
                  //+now call add_word to add thisword into the list
                  printf(" Add:%s ", thisword);
                  mywords = add_word(mywords, thisword);
                  printf(" Added:%s\n", mywords-&gt;str);
  
                  k=0;
              }
          }
          else
          {
              word = 1;
              //make ch lowercase
              //ch = putchar(toupper(ch));
              //+?? //make the kth character of thisword equal to ch
              thisword[k] = ch;
              thisword[k] = putchar(tolower(thisword[k]));
              k++;
          }
      }
      if (word == 1) 
      {
          thisword[k] = '\0';
          //add thisword into the list
          printf("Last Word:%s ", thisword);
          mywords = add_word(mywords, thisword);      
      }
  
      words *currword;
      printf("printing list\n");
  
      //+Traverse list and print each word and its count to outputfile
      //+output file is second parameter being passed
  
      myOutput = fopen(outputfile, "w+");  //+first parameter is input file
      if (myOutput == 0) 
      {
          printf("output file not opened \n");
          return 1;
      }
      else 
      {
          printf("output file opened \n");
      }
  
      currword = mywords;
  
      while (currword-&gt;next != NULL)
      {
          //add word name then word count to file, then move to next
          fprintf(myOutput, "%s %d \n", currword-&gt;str, currword-&gt;count);
          printf("%s ", currword-&gt;str);
          currword = currword-&gt;next;
      }
  
      return 0;
  
  }
  </code></pre>