Number of times one string occurs within another in C <p>I have written a function that uses the strstr() function to determine if string2 has any matches in string1. This works fine (I also convert both strings to lower case so that I can do a case-insensitive match.). However, strstr() only finds the <em>first</em> time the match occurs. Is there a way to use it to find <em>each</em> time a match occurs? For example:</p> <pre><code>string1[] = "ABCDEFABC"; string2[] = "ABC"; </code></pre> <p>Would return 0 as a match is found in position 0, and 6 as it's found again in position 6?</p> <p>Here's the original function as written (including call to function in main):</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;stdlib.h&gt; char *strstrnc(const char *str1, const char *str2); int main() { char buf1[80],buf2[80]; printf("Enter the first string to be compared: "); gets(buf1); printf("Enter the second string to be compared: "); gets(buf2); strstrnc(buf1,buf2); return 0; } char *strstrnc(const char *buf1, const char *buf2) { char *p1, *ptr1, *p2, *ptr2, *loc; int ctr; ptr1 = malloc(80 * sizeof(char)); ptr2 = malloc(80 * sizeof(char)); p1 = ptr1; p2 = ptr2; for (ctr = 0; ctr &lt; strlen(buf1);ctr++) { *p1++ = tolower(buf1[ctr]); } *p1 = '\0'; for (ctr = 0; ctr &lt; strlen(buf2);ctr++) { *p2++ = tolower(buf2[ctr]); } *p2 = '\0'; printf("The new first string is %s.\n", ptr1); printf("The new first string is %s.\n", ptr2); loc = strstr(ptr1,ptr2); if (loc == NULL) { printf("No match was found!\n"); } else { printf("%s was found at position %ld.\n", ptr2, loc-ptr1); } return loc; } </code></pre>