Answers for "C count occurrences of a substring"

C
0

C count occurrences of a substring

#include <stdio.h>
#include <string.h>
 
int count(const char *str, const char *sub) {
    int sublen = strlen(sub);
    if (sublen == 0) return 0;
    int res = 0;
    for (str = strstr(str, sub); str; str = strstr(str + sublen, sub))
        ++res;
    return res;
}
 
int main() {
        printf("%d\n", count("sea shells sea shells on the sea shore", "sea"));
}
Posted by: Guest on May-14-2021

Code answers related to "C count occurrences of a substring"

Code answers related to "C"

Browse Popular Code Answers by Language