简体   繁体   中英

recursive strstr function in c

I wrote my recursive strstr but the problem is that if I have this code:

char *str = "Yesterday all my troubles seemed so far away";
char *subStr[6] = { "Yes", "all", "my", "see", "far", "day" };
char *res;
int i;
printf("%s\n", str);
res = str;
for (i = 0; i<6; i++)
{
    printf("%s\n", subStr[i]);
    res = recursiveStrStr(res, subStr[i]);
    if (res == 0)
    {
        printf("The specified text is not found.\n");
        break;
    }
    else
        printf("The found text: %s\n", res);
}

My strstr return str very well till it gets to i=5 so the substr is "day" and str that left is "far away" and it should return 0 - that means text not found but it returns str dont understand why ?

my strstr code (should be recursive):

int recursiveStrStr(char * str, char *substr)
{

    if (str == NULL  )
        return 0;
    else if (strncmp(str, substr, strlen(substr)) == 0)
        return str;
    else 
        return(recursiveStrStr(str+1, substr));

}

It is also possible to write a recursive strstr without calling any other functions but strstr itself:

char *RecStrStr(const char *haystack, const char *needle)
{
    assert(haystack);
    assert(needle);

    if(*needle == 0)
        return (char *)haystack;

    if(*haystack == 0)
        return NULL;

    if(*haystack == *needle &&
        RecStrStr(haystack + 1, needle + 1) == haystack + 1)
        return (char *)haystack;

    return RecStrStr(haystack + 1, needle);
}

Basically, there are two types of recursive calls:

  1. Needle and haystack current chars match, in which case you would advance both pointers to compare the next chars.
  2. The current char of needle does not match the current char of haystack, in which case you would only advance the position of haystack.

If the null termination was reached, it is because needle is not a substring of haystack, and NULL is returned.

If the null termination of needle was reached, it is because haystack and needle matched consecutively, and a pointer to the current haystack position is returned.

Why? This is where things get a bit more complicated - in order to not return a positive answer when needle is a non-consecutive substring of haystack, we need to make sure that the return value of the next match is the pointer following the current following (this is the second condition in the third if).

If needle is indeed a substring of haystack, the return value will be the pointer in which the matching began, as wanted.

我想它应该是(*str == NULL)

You need another clause for returning "not found".

if ( *str == '\0' )
   return NULL;

Without that, you keep in incrementing str until you access out of bounds memory.

Also, I would change the return type of the function to char* .

char* recursiveStrStr(char * str, char *substr)
{
   if (str == NULL  )
      return NULL;

   if ( *str == '\0' )
      return NULL;

   if (strncmp(str, substr, strlen(substr)) == 0)
      return str;

   return(recursiveStrStr(str+1, substr));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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