title: Incorrect usage of a for loop in strstr() in C
body: <p>I am trying to learn string handling in C. I wrote out a program, which stores some music tracks, and helps the user to check if the song he/she has in mind, exists in the tracks stored. This is done by asking the user to enter a string of characters. The program then uses the strstr() function to check if the word/text entered matches with any word/text on any track, and if so, it displays the track number and name.</p>
  
  <p>The code which I have written functions properly on the outset. But there's a big flaw, and it is due to incorrect usage of the for loop. I'll show the program and two pieces of output first- </p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  
  //Define tracks array
  char tracks[][80]={     //[][80] is a 2D array- for storing tracks and the size of each track, which is 80 at max
  "I left my heart in Harvard med school",
  "Newark, Newark- a wonderful town",
  "Dancing with a dork",
  "From here to maternity",
  "The girl from Iwo Jima",
  };
  
  //Define function to search for text in tracks array
  
  void findTrack(char search_for[])
  {
      int i;
      for(i = 0; i &lt; 5; i++)
      {
         if(strstr(tracks[i], search_for))    
         {
            printf("\n Match found\n");       
            printf("\n Track[%i]: %s\n", i, tracks[i]);
            break;
         }
         else
             printf("\n No matching tracks found!");
         }
  }
  
  int main()
  {
     char search_for[80];
     printf("\n Enter the text to search:\n");
     scanf("%79s", search_for);
     findTrack(search_for);
     return 0;
  }
  </code></pre>
  
  <p>1st Output- </p>
  
  <pre><code> Enter the text to search:
  Harvard
  
   Match found
  
   Track[0]: I left my heart in Harvard med school
  </code></pre>
  
  <p>As you can see, this output is correct. The word <code>Harvard</code> was present in the first track I stored in the <code>tracks</code> array. This was found in the first iteration of the <code>for loop</code> inside the <code>findTrack</code> function, hence the correct output.</p>
  
  <p>However, if I run the program again, and this time I give the text to search as <code>town</code>, the resulting output is - </p>
  
  <p>2nd Output- </p>
  
  <pre><code>Enter the text to search:
  town
  
   No matching tracks found!
   Match found
  
   Track[1]: Newark, Newark- a wonderful town
  </code></pre>
  
  <p>This happened because on the first iteration of the <code>for</code> loop ie: <code>tracks[0]</code>, the <code>strstr</code> did not find any match, so it printed the statement in the <code>else</code> part of the loop. When the loop completed iteration 1, for <code>tracks[1]</code>, a match was found in <code>town</code>, and hence it printed the <code>if</code> part of the loop. </p>
  
  <p>Similarly, if I run the program again and give <code>Jima</code> as the text to search, the output is-</p>
  
  <pre><code> Enter the text to search:
  Jima
  
   No matching tracks found!
   No matching tracks found!
   No matching tracks found!
   No matching tracks found!
   Match found
  
   Track[4]: The girl from Iwo Jima 
  </code></pre>
  
  <p>I think the flaw is due to the incorrect for loop. But I'm not sure how to do it right using a for loop. Any suggestions on correcting this would be highly appreciated.
  Thank You!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>In my code, I'm using a <code>for</code> loop with <code>i&lt;5</code>because I know that there are <code>5 tracks</code> stored in my program. However, a better way to code would be to run a loop irrespective of/in cases when the number of tracks is not known. This could happen in a scenario when my number of tracks change, so it would be inefficient to keep changing the for loop each time. Any suggestions on how to implement this change?</p>