#include <stdio.h>
#include <string.h>
// Function to extract content between specified tags
void extractContentBetweenTags(const char* str, const char* tagName, char* result) {
char startTag[64], endTag[64];
sprintf(startTag, "<%s>", tagName); // Construct the opening tag
sprintf(endTag, "</%s>", tagName); // Construct the closing tag
const char* start = strstr(str, startTag); // Find the opening tag
if (start) {
start += strlen(startTag); // Move pointer to the content after the opening tag
const char* end = strstr(start, endTag); // Find the closing tag
if (end) {
size_t length = end - start; // Calculate the length of the content
strncpy(result, start, length); // Copy the content to the result buffer
result[length] = '\0'; // Null-terminate the string
return;
}
}
// If tags are not found, return an empty string
strcpy(result, "");
}
int main() {
const char* str = "abcd <tag>efg hijk lmnop</tag> qrst uvwxyz";
const char* tagName = "tag";
// Buffer to hold the extracted content
char extractedcontent[256];
// Call the function to extract the substring
extractContentBetweenTags(str, tagName, extractedcontent);
if (strlen(extractedcontent) > 0) {
printf("Extracted content: %s\n", extractedcontent);
} else {
printf("No matching tags found.\n");
}
return 0;
}
/*
run:
Extracted content: efg hijk lmnop
*/