I am trying to find a similar function to strstr
that searches a substring starting from the end towards the beginning of the string.
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- how to split a list into a given number of sub-lis
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()
andrstrstr()
, which might work if we are lucky. Get rid of__restrict
for C++. You also might want to make the parametersconst
, 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:Here is one. Testing it is an exercise I'll leave to you :)
No. This is one of the places that the C++ std::string class has an obvious advantage -- along with
std::string::find()
, there's alsostd::string::rfind()
.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.