Is there a reverse function for strstr

后端 未结 17 2410
闹比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:18

    char* strrstr(char * _Str, const char * _SubStr)
    {
        const BYTE EQUAL=0;
        int i=0, src_len = strlen(_Str), find_len = strlen(_SubStr),
            tail_count=0;
    
        for(i=src_len; i>-1; i--)
        {
            if(_Str[i] == _SubStr[0] && tail_count >= find_len)
            {
                if(strncmp(&_Str[i], _SubStr, find_len) == EQUAL)
                {
                    return &_Str[i];
                }
            }
            tail_count++;
        }
        return NULL;    
    }
    

提交回复
热议问题