c语言中strstr通过指针

发布于 2024-09-18 15:50:15 字数 328 浏览 5 评论 0原文

这是我制作的 strstr 的标准代码吗???

char* fstrset(char *s,char *t)
{
    int b, i=0,j=0;

 while(*(s+i)!='\0')
 {
  if(*(t+j)=='\0')
   break;
  else if(*(s+i)==*(t+j))
   {
   i++;j++;b=1;
   }
  else
   { i++;b=0;j=0;
   }
 }

    if(b==0)
     return((char*)NULL);
    else if(b==1)
     return(s+i-j);
}

is this the standard code for strstr i made????

char* fstrset(char *s,char *t)
{
    int b, i=0,j=0;

 while(*(s+i)!='\0')
 {
  if(*(t+j)=='\0')
   break;
  else if(*(s+i)==*(t+j))
   {
   i++;j++;b=1;
   }
  else
   { i++;b=0;j=0;
   }
 }

    if(b==0)
     return((char*)NULL);
    else if(b==1)
     return(s+i-j);
}

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

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

发布评论

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

评论(10

命硬 2024-09-25 15:50:15

这就是标准对此的全部规定:

7.21.5.7 strstr 函数

剧情简介

 #include ; 
char *strstr(const char *s1, const char *s2); 

描述

strstr函数定位第一个
出现在指向的字符串中
字符序列的 s1
(不包括终止 null
字符)在指向的字符串中
s2。

退货

strstr函数
返回一个指向已定位的指针
字符串,或者空指针,如果
未找到字符串。如果 s2 指向 a
长度为零的字符串,函数
返回 s1。

因此,您似乎缺少参数上的 const 限定符。

至于样式,请注意,*(ptr+index)可以用ptr[index]替换,并且size_t是最好使用的类型用于索引指针。

至于是否是一种常见的实现方式,与GCC的代码进行比较:

char *
strstr (const char *s1, const char *s2)
{
  const char *p = s1;
  const size_t len = strlen (s2);

  for (; (p = strchr (p, *s2)) != 0; p++)
    {
      if (strncmp (p, s2, len) == 0)
    return (char *)p;
    }
  return (0);
}

This is all the standard has to say about it:

7.21.5.7 The strstr function

Synopsis

 #include <string.h> 
char *strstr(const char *s1, const char *s2); 

Description

The strstr function locates the first
occurrence in the string pointed to by
s1 of the sequence of characters
(excluding the terminating null
character) in the string pointed to by
s2.

Returns

The strstr function
returns a pointer to the located
string, or a null pointer if the
string is not found. If s2 points to a
string with zero length, the function
returns s1.

So, it looks like you're missing const qualifiers on arguments.

As for style, note that *(ptr+index) can be replaced by ptr[index], and size_t is the best type to use for indexing a pointer.

As for being a common way to implement it, compare with GCC's code:

char *
strstr (const char *s1, const char *s2)
{
  const char *p = s1;
  const size_t len = strlen (s2);

  for (; (p = strchr (p, *s2)) != 0; p++)
    {
      if (strncmp (p, s2, len) == 0)
    return (char *)p;
    }
  return (0);
}
怎言笑 2024-09-25 15:50:15

你的代码有问题。给定:

char *haystack = "fififi-trixabelle";
char *needle = "fifi-trixabelle";

fstrset(haystack,needle)返回错误地返回NULL

Your code is buggy. Given:

char *haystack = "fififi-trixabelle";
char *needle = "fifi-trixabelle";

fstrset(haystack, needle) returns incorrectly returns NULL.

踏雪无痕 2024-09-25 15:50:15

除了caf提到的bug之外,还有其他的bug:

1)未初始化b。如果s指向'\0',则可能会到达右大括号,从而省略任何返回语句。

2) 如果字符匹配到 s 指向的字符串末尾,则不会检查 t 指向的字符串是否也结束。

Besides the bug mentioned by caf there are others:

1) Uninitialized b. If s points to '\0', closing brace may be reached, omitting any return statements.

2) If characters match up to the end of string pointed to by s there is no check if the string pointed to by t ends too.

痴意少年 2024-09-25 15:50:15

这是做什么的?看起来像是胡言乱语。为什么要添加指针并将它们与整数混合?抱歉,但这整件事没有意义。

回答你的问题,我不这么认为。但如果你编译它并运行它,那么是的。

好吧,当你仔细观察时,你的代码确实有意义。是的,它看起来确实可以编译,如果这就是您所说的标准代码的意思。

What does this do? It looks like gibberish. Why adding pointers, and mixing them with ints? Sorry, but the whole thing doesn't make sense.

And to answer your question, i don't think so. But if you compile it and it runs, then yes.

Okay, your code does make sense when you look at it closer. Yes, it does look like it will compile, if thats what you mean by standard code.

眼中杀气 2024-09-25 15:50:15
inline char* strstr(char* __s1, const char* __s2)
{
    return __builtin_strstr(const_cast<const char*>(__s1), __s2); 
}
inline char* strstr(char* __s1, const char* __s2)
{
    return __builtin_strstr(const_cast<const char*>(__s1), __s2); 
}
油焖大侠 2024-09-25 15:50:15

快速通读似乎表明代码可以工作(可能存在无法工作的边缘情况)。你告诉我们,这有效吗?

但为什么要这么做呢?只需调用 strstr

a quick read through seems to show that the code works (there are probably edge cases that dont work). You tell us, does it work?

But why do it? just call strstr

满地尘埃落定 2024-09-25 15:50:15

没有“标准代码”,只有标准结果。

标准 C 库中的任何实现都不太可能使用数组索引,因此您的代码不太可能与逐行细节中的任何实现相匹配。

There is no 'standard code', just the standard result.

It is unlikely that any implementation in a standard C library uses array indexing, so it is unlikely that your code matches any implementation in line-by-line detail.

定格我的天空 2024-09-25 15:50:15
char* fstrstr(char *s1,char *s2)
{
 int i=0,flag=0;
 char *s4,*s3;
// s4 for retaining the value of s2
 s4 = s2;
 while(*s1 != '\0' && *s2 != '\0')
 {
  if(*s1 == *s2)
  {
   *(s3+i) = *s1;
   s2++;
   s1++;
   i++;
   flag = 1;
  }
  else
  {
   i = 0;
   s1++;
//   Initialize s2 again from its address
   s2 = s4;
   flag = 0;
  }
 }
 if(flag == 1)
 {
  while(*s1 != '\0')
  {
   *(s3+i) = *s1;
   i++;
   s1++;
  }
  *(s3+i) = '\0';
 }
 if(flag == 1)
  return (s3);

 if(flag==0)
 {
  *s3 = NULL;
  return (s3);
 }
}
char* fstrstr(char *s1,char *s2)
{
 int i=0,flag=0;
 char *s4,*s3;
// s4 for retaining the value of s2
 s4 = s2;
 while(*s1 != '\0' && *s2 != '\0')
 {
  if(*s1 == *s2)
  {
   *(s3+i) = *s1;
   s2++;
   s1++;
   i++;
   flag = 1;
  }
  else
  {
   i = 0;
   s1++;
//   Initialize s2 again from its address
   s2 = s4;
   flag = 0;
  }
 }
 if(flag == 1)
 {
  while(*s1 != '\0')
  {
   *(s3+i) = *s1;
   i++;
   s1++;
  }
  *(s3+i) = '\0';
 }
 if(flag == 1)
  return (s3);

 if(flag==0)
 {
  *s3 = NULL;
  return (s3);
 }
}
书信已泛黄 2024-09-25 15:50:15

没有“标准代码”,只有标准结果。

标准 C 库中的任何实现都不太可能使用数组索引,因此您的代码不太可能与行实现中的任何实现相匹配。

There is no "standard code", only standard results.

It is unlikely that any implementation in the standard C library will use array indexes, so your code is unlikely to match any implementation in the line implementation.

绝不服输 2024-09-25 15:50:15
    char *strstr(const char *s1, const char *s2) {
      char *a = s1, *b = s2;
      for (;;)
        if      (!*b)          return (char *)s1;
        else if (!*a)          return NULL;
        else if (*a++ != *b++) {a = ++s1; b = s2;}
    }
    char *strstr(const char *s1, const char *s2) {
      char *a = s1, *b = s2;
      for (;;)
        if      (!*b)          return (char *)s1;
        else if (!*a)          return NULL;
        else if (*a++ != *b++) {a = ++s1; b = s2;}
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文