Here’s a pretty straight forward recursive implementation. Sadly, it’s not tail recursive though.
Full Implementation:
char *rfind(char* str, char ch) {
if (*str == '\0')
return NULL;
char * pos = rfind(str + 1, ch);
if (pos != NULL)
return pos;
if (*str == ch)
return str;
return NULL;
}
Base Case: str
is a string of zero characters:
if (*str == '\0')
return NULL;
Recursive Call:
char * pos = rfind(str + 1, ch);
Determine if we want the result of the remainder of the string, or the current position:
if (pos != NULL)
return pos;
if (*str == ch)
return str;
return NULL;
4
solved Recursive function to reverse find [closed]