Using strstr () multiple times on the same line in C

I am trying to write code that fetches all words / lines between tags and tags using strstr. But it seems like it was just stuck on the first line, which was "fast". How can I get the code to continue running after the first line has been fetched?

#include <stdio.h>
#include <string.h>

int main()
{

    char feed[] = "The <item> quick </item> brown <item> fox </item> jumps <item> over </item> the <item> lazy dog </item>";


    const char needle[] = "<item>";
    const char popo[] = "</item>";
    char *ret;
    char *ter;
    int n;
    n = 0;

    while (feed[n] != '\0')
    {
        ret = strstr(feed, needle)+6;
        ter = strstr(ret, popo);
        size_t len = ter - ret;
        char *res = (char*)malloc(sizeof(char)*(len+1));
        strncpy(res, ret, len);
        res[len] = '\0';

        printf("%s",res);
        n++;
    }
    return 0;
}

      

+3


source to share


2 answers


You need the pointer to ret

point to the current position in the string and increment it in length on each iteration and then pass ret

to the first one strstr()

instead of feed

checking this implementation



#include <stdio.h>
#include <string.h>

int main()
{

    char       feed[]   = "The <item> quick </item> brown <item> fox </item> "
                          "jumps <item> over </item> the <item> lazy dog </item>";
    const char needle[] = "<item>";
    const char popo[]   = "</item>";
    char      *head;
    int n;
    n = 0;

    head = feed;
    while (feed[n] != '\0')
    {
        char  *tail;
        char  *copy;
        size_t length;

        head = strstr(head, needle);
        /*            ^ always start at the current position. */
        if (head == NULL)
         {
            fprintf(stderr, "Invalid input...???\n");
            return -1;
         }
        tail   = strstr(head, popo);
        length = tail - head - 6;
        head  += 6;
        if (length < 0)
         {
            fprintf(stderr, "Invalid input...???\n");
            return -1;
         }
        copy = malloc(length + 1);
        if (copy != NULL)
         {
            memcpy(copy, head, length);
            copy[length] = '\0';

            printf("*%s*\n", copy);
            /* If you are not going to keep it, free it */
            free(copy);
         }
        head += length; /* <-- this is the imprtant thing */
        n++;
    }
    return 0;
}

      

+1


source


In this line:

ret = strstr(feed, needle)+6;

      

You always start your search at the beginning of the string feed

. You need to pass a different starting point at strstr

which you already have at ter

. Therefore, you should be able to do something like this:

ter = feed;
while (ter != NULL) 
{
     ret = strstr(ter, needle) + 6;
...

      



At the same time, the beginning of your search will continue to move down the line feed

.

There are some other problems in the code:

  • strstr()

    may return NULL if it doesn't find a match - you need to check this or the program will crash.
  • You need free()

    memorymalloc()

  • As @iharob points out " Don't quitmalloc()

    "
+1


source







All Articles