Logo

Programming-Idioms

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

Idiom #82 Count substring occurrences

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

count = s.count(t)

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

Overlapping occurrences are counted.
This destroys the pointer s.

New implementation...
< >
deleplace