C中有没有找到子串重叠的函数?

发布于 2024-11-08 11:13:48 字数 278 浏览 0 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初见你 2024-11-15 11:13:48

使用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;
}

输出:

这是一个示例字符串

注意:

整个子字符串将被匹配;请注意,strstr 不进行部分匹配。

Use strstr. Example from link:

/* 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;
}

Output:

This is a sample string

Note:

The entire substring will be matched ; note that strstr does not do partial matches.

腻橙味 2024-11-15 11:13:48
char *strstr(const char *str1, const char *str2);

函数 strstr() 查找
str2 在 str1 中出现,并且
返回指向出现的指针
str1 中的 str2。如果没有找到匹配项,则
返回一个空指针。

char *strstr(const char *str1, const char *str2);

The function strstr() finds the
occurrence of str2 in the str1 and
returns the pointer to occurrence of
str2 in str1. If no match found, then
a null pointer is returned.

你的呼吸 2024-11-15 11:13:48

资格:该函数计算类型的重叠

string1  
    ng1randomcharacters

在本例中,重叠为 3

// Traverses s1 from left to right and s2 from left to right, looking for overlap
int get_overlap(char *s1, char *s2)
{
   int u2 = strlen(s2)-1;
   int p1 = strlen(s1)-1;
   int p2 = 0;

   while( p1>=0 && p2<=u2 )
   {
      if (s1[p1--] != s2[p2++])
      {
         --p2; // no overlap, so correct for incremented value
         break;
      }
   }

   return(p2);
}

Qualification: This function computed overlaps of the type

string1  
    ng1randomcharacters

In this case, the overlap is 3

// Traverses s1 from left to right and s2 from left to right, looking for overlap
int get_overlap(char *s1, char *s2)
{
   int u2 = strlen(s2)-1;
   int p1 = strlen(s1)-1;
   int p2 = 0;

   while( p1>=0 && p2<=u2 )
   {
      if (s1[p1--] != s2[p2++])
      {
         --p2; // no overlap, so correct for incremented value
         break;
      }
   }

   return(p2);
}
栀子花开つ 2024-11-15 11:13:48

没有内置函数,但编写起来非常简单:

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文