title: Searching for a word in a linked list in C
body: <p>My code is supposed to read  file and store its contents in a linked list. Then, search for the user input word and print its location. Here is my try on it. I am kind of confused with the strcmp as well. Another thing i am not sure about is how to print the node (index) in the linked list where the word is found.</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  struct list {
      char *string;
      struct list *next;
  };
  
  typedef struct list LIST;
  
  int main() {
      FILE *fp;
      char line[128];
      LIST *current, *head;
  
      head = current = NULL;
      fp = fopen(&quot;words.dat&quot;, &quot;r&quot;);
  
      while(fgets(line, sizeof(line), fp)){
          LIST *node =(LIST*) malloc(sizeof(LIST));
          node-&gt;string = strdup(line);//note : strdup is not standard function
          node-&gt;next =NULL;
  
          if(head == NULL){
              current = head = node;
          } else {
              current = current-&gt;next = node;
          }
      }
  
  
      
      
      fclose(fp);
      //test print
      for(current = head; current ; current=current-&gt;next){
          printf(&quot;%s&quot;, current-&gt;string);
      }
      //searching for a word
      char string[50];
      printf(&quot;\nEnter the word to be searched\n&quot;);
      scanf(&quot;%s&quot;,&amp;string);
      LIST *tp1;
      int flag;
      tp1 = head;
      while(tp1 != NULL)
      {
  
          if(strcmp(tp1-&gt;string,string)==0)
              {
                  flag = 0;
                  printf(&quot;%d&quot;,(tp1-&gt;string)) ;
                  break;
              }
          tp1 = tp1-&gt;next;
      }
      if (flag)
          printf(&quot;\nThe name to be searched is not in the linked list\n&quot;);
      else
          printf(&quot;\nThe name to be searched is in the linked list\n&quot;);    
  
      return 0;    
  }
    
  </code></pre>