#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
bool strtextafter(const char *haystack, const char *needle, char **result) {
*result = NULL;
const char *h = haystack;
const char *n = needle;
while (*n && *h && *h == *n) {
h++;
n++;
}
if (*n != '\0') {
return false;
}
size_t rlen = strlen(h);
*result = (char *) malloc(rlen + 1);
if (*result == NULL) {
return false;
}
memcpy(*result, h, rlen + 1);
return true;
}
int main() {
char *result;
if (strtextafter("/images/qe3nb", "/images/", &result)) {
printf("%s\n", result);
free(result);
} else {
printf("No matches!\n");
}
return 0;
}
/** Cтарая версия с избыточной итерацией по символам strlen и stricmp
bool strtextafter(const char *haystack, const char *needle, char **result) {
size_t hlen = strlen(haystack);
size_t nlen = strlen(needle);
*result = NULL;
if (nlen > hlen) {
return false;
}
if (strncmp(haystack, needle, nlen) != 0) {
return false;
}
*result = (char *) malloc((hlen - nlen + 1) * sizeof(char));
if (*result == NULL) {
return false;
}
haystack += nlen;
char *dest = *result;
while (*haystack) {
*dest++ = *haystack++;
}
*dest = '\0';
return true;
}
*/