Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • C

Idiom #82 Count substring occurrences

Find how many times string s contains substring t.
Specify if overlapping occurrences are counted.

#include <string.h>
unsigned n;
for (n = 0; s = strstr(s, t); ++n, ++s)
	;

Overlapping occurrences are counted.
This destroys the pointer s.
(count (re-seq t s))

Ensure t is a regex pattern #""

New implementation...
< >
deleplace