Hi, I'm trying to write a small code for an assignment which the void String Tokenizer will get a line input, and take out those delimiters from the original string. Then pass each string without the delimiters back to the struct.
For an example:
Line in is: Today, Friday 23, is a crazy day.
Delimiters is: " ,." /*space, comma and period/*
The stuct will get total of 7 words: "Today", "Friday", "23", "is" ..... "day".
This is the codes for that little function:
I tested each string passed back into the struct, and first 2 strings are fine, but all other strings are really messed up, with weird characters at the end. At the same time, it skipped last 2 words. Is something wrong with my algorithm? Is there a alternate algorithm for doing this?
Thanks.
For an example:
Line in is: Today, Friday 23, is a crazy day.
Delimiters is: " ,." /*space, comma and period/*
The stuct will get total of 7 words: "Today", "Friday", "23", "is" ..... "day".
This is the codes for that little function:
Code:
void constructStringTokenizer(struct StringTokenizer* tokenizer, const char line[], const char delimiters[])
{
char* temp;
int i, j, k;
k=0;
for (i =0; i < strlen(line); i++)
{
if (k == 0)
temp = malloc (sizeof(char)*MAXLEN+1);
// printf ("%d\n", i);
for (j = 0; j < 3; j++)
{
// printf("Went through one delimiter\n");
if (line[i]==delimiters[j])
{
i++;
tokenizer->tokens[tokenizer->count] = (char*)malloc(sizeof(char)*(k+1));
strcpy (tokenizer->tokens[tokenizer->count++],temp);
strcpy (temp, "");
k = 0;
printf ("One string copied\n");
break;
}
}
temp[k]=line[i];
k++;
printf ("%s\n", temp);
}
// free (temp);
}
Thanks.
Comment