开发者

Is there a C function to find the second occurrence of substring in string?

开发者 https://www.devze.com 2022-12-19 00:26 出处:网络
Is there a C function to find the second occurrence of sub-string in string? i.e. String - \"213 File status 550 Access Denie开发者_Go百科d. 550 Access Denied.\"

Is there a C function to find the second occurrence of sub-string in string?

i.e. String - "213 File status 550 Access Denie开发者_Go百科d. 550 Access Denied."

This function would return "550 found twice"....


Use strstr. Since strstr returns a pointer to the first occurrence of the needle, you can use the result to find the next occurrence.

For example, to count occurrences of the string "550":

#include <string.h>

int count_550s(const char *str)
{
    const char *ptr = str;
    int count = 0;
    while ((ptr = strstr(ptr, "550")) != NULL) {
        // ptr is pointing at "550...", so we skip
        // over the "550".
        ptr += 3;
        count++;
    }
    return count;
}


strstr() returns a pointer to found string, so you can use this to do a second search (bump it by one if first one found).


You can do that by calling strstr in a loop (haystack refers to the string you are searching in "213 File status 550 Access Denied. 550 Access Denied." while needle refers to the string you are searching for "550"):

unsigned int count = 0;
const char *next = haystack
while ((next = strstr(next, needle)) != NULL)
{
    ++count;
    ++next;
}

If two instances of you needle can overly one another (so you are looking for "555", and the string has "5555" in it, this will count two instances). If you don't want that, you should change ++next to next += strlen(needle);


Have a look at this 'strtok' function found here and on the GNU website here. Here's another link about the strtok function found here also.

Hope this helps, Best regards, Tom.


You could do it like this:

char *yourText = "213 File status 550 Access Denied. 550 Access Denied.";

char *found = strstr(yourText, "550 Access Denied");

if(found != NULL && strstr(found+1, "550 Access Denied"))
{
    // second occurrence found
}

But if your intention is to find duplicates in general (i.e. without knowing the search string), you should use a regular expression library which supports matching the same group pattern twice.


If you know the substring you in which you are interested, you could repeatedly call strstr to find it. If you don't know the substring beforehand (which I suspect is the case), then you might be wanting to find the maximal length repeating string. For that, suffix arrays might be a solution.


No, but you can build one yourself.

const char *find_second_substring(const char *findme,const char *str)
{
   const char *tmp;
   if((tmp = strstr(str,findme)) != null) {
     tmp = strstr(tmp  +strlen(findme),findme);
   }

  return tmp;
}


  1. Write a function, str_search(char* s1,char* s2, int n) , that takes two strings and an integer, as arguments and returns a pointer to the nth occurrence of first string s1 in the second string s2, or NULL if nth occurrence is not present.
0

精彩评论

暂无评论...
验证码 换一张
取 消