c - C 中是否有查找子字符串重叠的函数?

标签 c arrays function substring

在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 值的。

最佳答案

使用strstr .来自链接的示例:

/* strstr example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="This is a simple string";
  char * pch;
  pch = strstr (str,"simple");
  strncpy (pch,"sample",6);
  puts (str);
  return 0;
}

输出:

This is a sample string

注意事项:

将匹配整个 子串;请注意 strstr 不会进行部分匹配。

关于c - C 中是否有查找子字符串重叠的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6025548/

相关文章:

c - C中的指针与指向指针的指针

javascript:在函数完成处理之前执行函数的顺序 - 嵌套函数返回值?

PHP检查用户是否使用函数登录

javascript - 更新数组中的多个对象

jquery函数: is a radio checked/same id

c - 如何让 CLion 调试器/控制台告诉我我的程序运行了多少秒?

c - 流浪\[反斜杠]在C代码中有什么意义吗?

c - 是否可以从 C 语言函数写入 word 文件?

JavaScript 范围 : preserve object in array in function

javascript - 为什么我不能用forEach来遍历一棵树?