C3 said:
I have to process some data in C
that is given to me as a char * array. I
have a fairly large number of substrings (well, they're not actually
printable, but let's treat them as strings)
that I have to search for in the data.
I need to keep a count of how often each of these
substrings occurs in my original data and
then print it out at the end.
This is a fairly mundane task,
but since I have so many substrings, it's a
pain having to #define them all.
Can anybody suggest an efficient way of
doing this task?
BTW, I have to use C. If I could use Perl, I'd be in heaven.
/* BEGIN new.c output */
The substring "string", occurs 4 times.
The substring "C", occurs 3 times.
The substring "kibo", occurs 0 times.
The substring "hen", occurs 1 times.
The substring "of", occurs 5 times.
/* END new.c output */
/* BEGIN new.c */
#include <stdio.h>
#include <string.h>
int main(void)
{
struct substring {
char *string;
long unsigned count;
} substring[] = {{"string"},{"C"},{"kibo"},{"hen"},{"of"}},
*str_ptr;
char *data[] = {
"I have to process some data in C that is given to me "
"as a char * array.\n I have a fairly large number of "
"substrings\n(well, they're not actually printable, "
"but let's treat them as strings)\nthat I have to search "
"for in the data.",
"I need to keep a count of how often each of these "
"substrings occurs in my original data and then print it "
"out at the end.",
"This is a fairly mundane task, but since I have so many "
"substrings, it's a pain having to #define them all.\n"
"Can anybody suggest an efficient way of doing this task?\n",
"BTW, I have to use C.\n"
"If I could use Perl, I'd be in heaven.\ncheers,"
};
size_t nstring, ndata;
char **data_ptr, *ptr;
long unsigned count;
nstring = sizeof substring / sizeof *substring;
for (str_ptr = substring; nstring-- != 0; ++str_ptr) {
count = 0;
data_ptr = data;
ndata = sizeof data / sizeof *data;
while (ndata-- != 0) {
ptr = *data_ptr;
ptr = strstr(*data_ptr, str_ptr -> string);
while (ptr != NULL) {
++count;
ptr = strstr(ptr + 1, str_ptr -> string);
}
++data_ptr;
}
str_ptr -> count = count;
}
puts("/* BEGIN new.c output */\n");
nstring = sizeof substring / sizeof *substring;
for (str_ptr = substring; nstring-- != 0; ++str_ptr) {
printf("The substring \"%s\", occurs %lu times.\n",
str_ptr -> string, str_ptr -> count);
}
puts("\n/* END new.c output */");
return 0;
}
/* END new.c */