Is there a reverse function for strstr

后端 未结 17 2224
闹比i
闹比i 2020-12-18 18:28

I am trying to find a similar function to strstr that searches a substring starting from the end towards the beginning of the string.

17条回答
  •  醉梦人生
    2020-12-18 19:20

    Here is the most minimal simple implantation that I could come up with. Unlike other implementations of this function it avoids the initial strstr call that some other people like user3119703 had.

    char * lastStrstr(const char * haystack,const char * needle){
        char*temp=haystack,*before=0;
        while(temp=strstr(temp,needle)) before=temp++;
        return before;
    }
    

提交回复
热议问题