Tutorial :How to find out what was the last reason to tokenize



Question:

I am using the following code to tokenize the string in C and using " ," to make tokens but i wanted to know when it make token of string when " " came and when "," occur in the string.

char *pch;  pch = strtok(buffer, ", ");  while (pch!=NULL) {      printf("%s\n", pch);      pch = strtok(NULL, " ,");  }  


Solution:1

As far as I know, strtok does not support that feature. However, you may do additional check: whenever pch is returned, see whether the first character of the remaining buffer has " " or ",". Then, you need to track down the remaining buffer, but this is your homework :)


Solution:2

The strtok and the CLIB does not expose this information. So there is no portable way to get it.

You may get the information you're looking for with a hack that works for your clib-implementation, but if you want something stable and portable the only choice you have is to implement a strtok-workalike that tells you the separator.

That task is not rocket-science btw...


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »