Search code examples
carraysfunctionsubstring

Is there a function in C to find a substring overlap?


in C, is there a function that when giver two strings, will return the substring overlap or size of the overlap? So like something that does:

char s1[5] = {cart};
char s2[4] = {car};
int overlap;
overlap = get_overlap(s1, s2); /*or have overlap be a string if it returns the overlap*.

and then overlap would be 3.

If not, how do i make one that will return the int value of the overlap.


Solution

  • There's no builtin function, but it's pretty simple to write:

    size_t overlap(const char *s1, const char *s2)
    {
        size_t i = 0;
    
        while (s1[i] && s2[i] && s1[i] == s2[i])
            i++;
    
        return i;
    }