title: C Program to count substrings
body: <p>Why does my program always skip the last substring count?</p>
  
  <p>eg1. String: <code>dbdbsnasdb dbdxx</code></p>
  
  <p>Substring: <code>db</code></p>
  
  <p>count: <code>4</code> (no error)</p>
  
  <p>eg2. String: <code>dbdbsnasmfdb</code></p>
  
  <p>Substring: <code>db</code></p>
  
  <p>count: <code>2</code> (supposed to be 3)</p>
  
  <p>** <code>#include &lt;stdio.h&gt;</code> only</p>
  
  <pre><code>int countSubstr(char string[], char substring[]) {
      int i, j;
      int count = 0;
      int subcount = 0;
      for (i = 0; i &lt;= strlen(string);) {
          j = 0;
          count = 0;
          while ((string[i] == substring[j])) {
              count++;
              i++;
              j++;
          }
          if (count == strlen(substring)) {
              subcount++;
              count = 0;
          } else
              i++;
      }
      return subcount;
  }
  </code></pre>
  
  <p>and why must I declare my <code>j</code> and <code>count</code> to be <code>0</code> in the for loop? is it because <code>j</code> has to remain as <code>0</code> (substring remains the same) whenever it loops?</p>