Is there a reverse function for strstr

2020-02-11 18:50发布

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

标签: c++ c string
17条回答
霸刀☆藐视天下
2楼-- · 2020-02-11 18:56

I don't know of one. One of the nice things about C is that if you write your own function, it's just as fast and efficient as the library ones. (This is totally not the case in many other languages.)

You could reverse the string and the substring, and then search.

Finally, the other thing people often do when the string library isn't good enough is to move to regular expressions.

Ok, I wrote both reverse() and rstrstr(), which might work if we are lucky. Get rid of __restrict for C++. You also might want to make the parameters const, but then you will need to cast the return value. To answer your comment question, you can get the index from the address of the substring by just substracting the original string pointer from it. OK:

#include <stdlib.h>
#include <string.h>

char *reverse(const char * __restrict const s)
{
  if (s == NULL)
    return NULL;
  size_t i, len = strlen(s);
  char *r = malloc(len + 1);

  for(i = 0; i < len; ++i)
    r[i] = s[len - i - 1];
  r[len] = 0;
  return r;
}

char *rstrstr(char *__restrict s1, char *__restrict s2)
{
  size_t  s1len = strlen(s1);
  size_t  s2len = strlen(s2);
  char *s;

  if (s2len > s1len)
    return NULL;
  for (s = s1 + s1len - s2len; s >= s1; --s)
    if (strncmp(s, s2, s2len) == 0)
      return s;
  return NULL;
}
查看更多
Deceive 欺骗
3楼-- · 2020-02-11 18:57
char * strrstr(char *_Str, char *_SubStr){
    char *returnPointer, *p;

    //find 1st occurence. if not found, return NULL
    if ( (p=strstr(_Str, _SubStr))==NULL)
        return NULL;

    //loop around until no more occurences
    do{
        returnPointer=p;
        ++p;
    }while(p=strstr(p, _SubStr));

    return returnPointer;
}
查看更多
SAY GOODBYE
4楼-- · 2020-02-11 18:57
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;    
}
查看更多
Anthone
5楼-- · 2020-02-11 18:58

Here is one. Testing it is an exercise I'll leave to you :)

查看更多
等我变得足够好
6楼-- · 2020-02-11 18:59

No. This is one of the places that the C++ std::string class has an obvious advantage -- along with std::string::find(), there's also std::string::rfind().

查看更多
神经病院院长
7楼-- · 2020-02-11 18:59

I think you can still do it using library functions.

1.Use strrev function to reverse the string.

2.Use strstr function to do whatever you want to do.

3.You can find start index (from reverse ) of the search string by subtracting start index of the search string from the length of original string.

查看更多
登录 后发表回答