Consider this function, I quickly wrote to find the last occurrence of a given char in a string and return it to an array of characters, which is physically a string:
size_t strlstchar(const char *str, const char ch)
{
char *chptr = strrchr(str, ch);
return chptr - str;
}
I just typed it here very quickly (I have not compiled or noticed yet) because I have questions about a few things.
For me, this seems like the simplest solution for finding which element of an array contains the last instance of a specific char, but I don't know how this works. I just did this after the strrchr documentation, so technically strrchr does all the work. I just can't imagine that this is the best way (in terms of performance) to achieve this, and was hoping that someone could make some contribution to what would be the best way to do this.
Is strrchr an effective way to do this? Or is strrchr best left to other use?
source
share