Jump to content

Count occurrences of a substring: Difference between revisions

m (Alphabetized)
Line 37:
3 2 2
</pre>
=={{header|C}}==
<lang C>#include <stdio.h>
#include <string.h>
 
int match(char *s, char *p, int overlap)
{
int c = 0, l = strlen(p);
 
while (*s != '\0') {
if (strncmp(s++, p, l)) continue;
if (!overlap) s += l - 1;
c++;
}
return c;
}
 
int main()
{
printf("%d\n", match("the three truths", "th", 0));
printf("overlap:%d\n", match("abababababa", "aba", 1));
printf("not: %d\n", match("abababababa", "aba", 0));
return 0;
}</lang>
 
=={{header|C++}}==
<lang cpp>#include <iostream>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.