本文介绍了通过在C语言中指针的strstr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是标准的code代表我的strstr做????

 的char * fstrset(字符* S,字符* T)
{
    INT B,I = 0,J = 0; 而(*(S + I)!='\\ 0')
 {
  如果(*(T + J)=='\\ 0')
   打破;
  否则,如果(*(S + I)== *(T + J))
   {
   我++; J ++; B = 1;
   }
  其他
   {I ++; B = 0; J = 0;
   }
 }    如果(二== 0)
     收益率((的char *)NULL);
    否则如果(B == 1)
     返回(S + I-J);
}


解决方案

这是所有的标准有什么看法吧:

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

这篇关于通过在C语言中指针的strstr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:36
查看更多