J
John Smith
I would like to parse a string into an array. I found on the net
the following codes which parse a string and print it. The result
is exactly what I want:
char * pch;
pch = strtok (buffer," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.");
}
To store the output to a string array, I created a two-dimensional
array and copied the output to the array:
char output[5][55];
char * pch;
int i=0;
pch = strtok (str," ");
while (pch != NULL)
{
strcpy(output,pch);
printf ("%s\n",pch);
printf("output[%d] is %s.\n";i,output[i++]);
pch = strtok (NULL, " ,.");
}
This doesn't work. What's in the output array is nothing like the
output (on screen). How should it be done?
the following codes which parse a string and print it. The result
is exactly what I want:
char * pch;
pch = strtok (buffer," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.");
}
To store the output to a string array, I created a two-dimensional
array and copied the output to the array:
char output[5][55];
char * pch;
int i=0;
pch = strtok (str," ");
while (pch != NULL)
{
strcpy(output,pch);
printf ("%s\n",pch);
printf("output[%d] is %s.\n";i,output[i++]);
pch = strtok (NULL, " ,.");
}
This doesn't work. What's in the output array is nothing like the
output (on screen). How should it be done?