tags:

views:

136

answers:

8

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);
}
A: 
Alexander Rafferty
A: 
inline char* strstr(char* __s1, const char* __s2)
{
    return __builtin_strstr(const_cast<const char*>(__s1), __s2); 
}
Svisstack
+3  A: 

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);
}
Potatoswatter
It's also worth looking at [glibc's implementation](http://sourceware.org/git/?p=glibc.git;a=blob;f=string/strstr.c;h=ef45f827584526e09c73426c5d63e3197f31a909;hb=HEAD), which uses a more complex algorithm to avoid the N^2 behaviour of the naive implementation.
caf
@caf Thanks, that's what I was looking for. I wonder why glibc isn't in the GCC source tree, and why GCC isn't using the faster implementation.
Potatoswatter
@Potatoswatter: It's because they're separate projects. You can use GCC with other `libc` s - in fact it's quite common (eg on Mac OS X, Solaris, BSD, ...) (and you can use `glibc` with other compilers, although they do have to be quite gcc-like and it's not as common a case).
caf
A: 

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

pm100
There certainly are cases that don't work - see my answer for one.
caf
ya it works i just want to implement the funtion. would call strstr next time :-)
SHEHROZ
A: 

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.

Jonathan Leffler
+1  A: 

Your code is buggy. Given:

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

fstrset(haystack, needle) returns incorrectly returns NULL.

caf
+1  A: 

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.

Maciej Hehl
A: 
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);
 }
}
Umair Khan