EXPLODE ROUTINE
Tarador
sah at gnu.ai.mit.edu
Wed May 1 00:49:48 AEST 1991
The following program is akin to the MUF primitive EXPLODE, if any of
you are familiar with MUCK. Anyway, it works like this:
x = explode(char *data, char *trigger, char **array);
Where x is an int, *data is a a data string, *trigger is the string to
explode on, and **array is filled by results.
So, x = explode("Hello.Test", ".", results);
That SHOULD put "Hello" in results[0] and "Test" in results[1]. But,
for some reason, I keep getting a bus error. Program follows:
int explode();
main()
{
int c,cc;
char **arr, x[50], trig[5];
gets(x); gets(trig);
cc = explode(x, trig, arr);
for (c=0;c<cc;++c)
printf("%s\n", arr[c]);
}
int explode(char *data, char *trigger, char **array)
{
int x;
int last=0, ctr=0;
char dummy[512];
for (x=0;x<strlen(data);++x) {
if (!strncmp(&data[x], trigger, strlen(trigger))) {
/* Here's the bus error*/ strncpy(array[ctr], &data[last], x);
last = x+strlen(trigger);
++ ctr;
}
}
return ctr;
}
Please post suggestions, etc., to sah at gnu.ai.mit.edu. Thanks.
--
+-------------------------------------------------------------------------+
| Tarador Dranon MIT: sah at gnu.ai.mit.edu |
| C-Guild: vidco%cguild%valnet at iuvax.cs.indiana.edu |
+-------------------------------------------------------------------------+
More information about the Comp.lang.c
mailing list