Pattern in string count for C
Drop the Intermediate String

You're allocating compareString and strcpying into it just so that you can use strcmp. But instead, there's also memcmp, which lets you compare from the original string directly:

Fix the loop condition

You're not really looping from 0 to stringLength, you're looping from 0 to stringLength - patternLength. Splitting up those concerns in two is confusing.

Adjusting i

You have:

i--;
i+=patternLength; /* non overlapping find */


We can do both in one:

i += patternLength - 1;


Better solution:

size_t count(char *string, char *pat) {
    size_t patternLength = strlen(pat);
    size_t stringLength = strlen(string);
    size_t count = 0;

    for (int i = 0; i &lt; stringLength - patternLength; i++) {
        if (memcmp(string+i, pat, patternLength) == 0) {
            count++;
            i += patternLength - 1;
        }

    }

    return count;
}