This is an example of how you can do it, it's not checking the input string integrity
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *extract(const char *const string, const char *const left, const char *const right)
{
char *head;
char *tail;
size_t length;
char *result;
if ((string == NULL) || (left == NULL) || (right == NULL))
return NULL;
length = strlen(left);
head = strstr(string, left);
if (head == NULL)
return NULL;
head += length;
tail = strstr(head, right);
if (tail == NULL)
return tail;
length = tail - head;
result = malloc(1 + length);
if (result == NULL)
return NULL;
result[length] = '\0';
memcpy(result, head, length);
return result;
}
int main(void)
{
char string[] = "<title>The Title</title>";
char *value;
value = extract(string, "<title>", "</title>");
if (value != NULL)
printf("%s\n", value);
free(value);
return 0;
}