How to find the number of occurrences of substring in a string?

I wrote this C code that can find the number of occurrences of a substring in a string. The problem is it overlaps meaning if I search for the letters pp in a string that has ppp the answer will be 2 occurrences instead of 1. I need help stopping this overlapping.

char* fullString = arguments[1];
    char* subString = arguments[2];
    int fullLength=strlen(fullString);
    int subLength=strlen(subString);
    int result = 0;

    for(int i=0;i<fullLength;i++){
        int check;
        for(check=0;check<subLength;check++)
            if (fullString[i+check]!=subString[check])
                break;

        if(check==subLength){
            result++;
            check=0;
        }
    }
    return result;


Read more here: https://stackoverflow.com/questions/64899228/how-to-find-the-number-of-occurrences-of-substring-in-a-string

Content Attribution

This content was originally published by Ammar Montaser at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: