#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **findAllLogsByDate(const char *logs, const char *targetDate, int *count) {
char *copy = strdup(logs); // make a modifiable copy
char *line = strtok(copy, "\n");
char **matches = NULL;
*count = 0;
while (line != NULL) {
if (strstr(line, targetDate) != NULL) {
matches = realloc(matches, (*count + 1) * sizeof(char *));
matches[*count] = strdup(line);
(*count)++;
}
line = strtok(NULL, "\n");
}
free(copy);
return matches;
}
int main(void) {
const char *logs =
"01/12/2023 - Log entry one.\n"
"17/03/2021 - Log entry two.\n"
"29/07/2019 - Log entry three.\n"
"05/11/2024 - Log entry four.\n"
"22/08/2020 - Log entry five.\n"
"14/02/2018 - Log entry six.\n"
"30/09/2022 - Log entry seven.\n"
"11/06/2017 - Log entry eight.\n"
"03/04/2025 - Log entry nine.\n"
"05/11/2024 - Log entry ten.\n";
int count = 0;
char **results = findAllLogsByDate(logs, "05/11/2024", &count);
for (int i = 0; i < count; i++) {
printf("%s\n", results[i]);
free(results[i]);
}
free(results);
return 0;
}
/*
run:
05/11/2024 - Log entry four.
05/11/2024 - Log entry ten.
*/