在C语言中,是否有一个函数在给定两个字符串时,返回子字符串重叠或重叠的大小?所以像这样:
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*.
然后重叠为3。
如果没有,我如何创建一个返回重叠int值的函数。
最佳答案
没有内置函数,但编写起来非常简单:
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;
}