#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function to find hyperlinks in a given HTML string
void findHyperlinks(const char *html) {
const char *pTagStart = "<p";
const char *pTagEnd = "</p>";
const char *aTagStart = "<a";
const char *aTagEnd = "</a>";
// Find the first <p> tag
const char *pStart = strstr(html, pTagStart);
while (pStart != NULL) {
// Find the corresponding </p> tag
const char *pEnd = strstr(pStart, pTagEnd);
if (pEnd == NULL) {
break; // No corresponding </p> tag found
}
// Extract the content between <p> and </p> tags
size_t contentLength = pEnd - pStart + strlen(pTagEnd);
char pContent = (char )malloc(contentLength + 1);
strncpy(pContent, pStart, contentLength);
pContent[contentLength] = '\0';
// Check if the <p> tag contains an <a> tag
const char *aStart = strstr(pContent, aTagStart);
while (aStart != NULL) {
// Find the corresponding </a> tag
const char *aEnd = strstr(aStart, aTagEnd);
if (aEnd == NULL) {
break; // No corresponding </a> tag found
}
// Extract the content between <a> and </a> tags
size_t linkLength = aEnd - aStart + strlen(aTagEnd);
char aContent = (char )malloc(linkLength + 1);
strncpy(aContent, aStart, linkLength);
aContent[linkLength] = '\0';
// Print the hyperlink content
printf("Hyperlink: %s\n", aContent);
// Free memory allocated for the hyperlink content
free(aContent);
// Find the next <a> tag within the <p> tag
aStart = strstr(aEnd + strlen(aTagEnd), aTagStart);
}
// Free memory allocated for the <p> tag content
free(pContent);
// Find the next <p> tag in the HTML string
pStart = strstr(pEnd + strlen(pTagEnd), pTagStart);
}
}
int main() {
// Sample HTML string for testing
const char *html = "<p>This is a <a href='https://example.com'>hyperlink</a> in a <a href='https://example2.com'>paragraph</a>.</p>"
"<p>Another <a href='https://example3.com'>link</a> in a second <a href='https://example4.com'>paragraph</a>.</p>";
// Find and print hyperlinks in the HTML string
findHyperlinks(html);
return 0;
}