简体   繁体   English

c语言通过指针strstr

[英]strstr through pointers in c language

is this the standard code for strstr i made????这是我制作的 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);
}

This is all the standard has to say about it:这就是标准要说的所有内容:

7.21.5.7 The strstr function 7.21.5.7 strstr函数

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. strstr 函数在 s2 所指向的字符串中定位字符序列(不包括终止空字符)的 s1 所指向的字符串中的第一次出现。

Returns退货

The strstr function returns a pointer to the located string, or a null pointer if the string is not found. strstr 函数返回指向所定位字符串的指针,如果未找到该字符串,则返回空指针。 If s2 points to a string with zero length, the function returns s1.如果 s2 指向长度为零的字符串,则函数返回 s1。

So, it looks like you're missing const qualifiers on arguments.因此,您似乎缺少参数上的const限定符。

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.至于样式,请注意*(ptr+index)可以替换为ptr[index] ,而size_t是用于索引指针的最佳类型。

As for being a common way to implement it, compare with GCC's code:至于常见的实现方式,对比一下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);
}

Your code is buggy.你的代码有问题。 Given:鉴于:

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

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

Besides the bug mentioned by caf there are others:除了 caf 提到的错误之外,还有其他错误:

1) Uninitialized b. 1) 未初始化 b. If s points to '\\0' , closing brace may be reached, omitting any return statements.如果s指向'\\0' ,则可以到达右大括号,省略任何 return 语句。

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. 2) 如果字符匹配到s指向的字符串的末尾,则不检查t指向的字符串是否也结束。

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. 对不起,但整件事没有意义。

\n

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.是的,它看起来确实可以编译,如果这就是您所说的标准代码的意思。

inline char* strstr(char* __s1, const char* __s2)
{
    return __builtin_strstr(const_cast<const char*>(__s1), __s2); 
}

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只需调用 strstr

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.标准 C 库中的任何实现都不太可能使用数组索引,因此您的代码不太可能逐行详细地匹配任何实现。

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

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.标准 C 库中的任何实现都不太可能使用数组索引,因此您的代码不太可能与行实现中的任何实现相匹配。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM