What does strtok return when it finds "nothing" between two delimiters? (It seams that it is not NULL).
I need to insert a character between two deliniters, but cannot find any if-condition for this case.
Thank you.
What does strtok return when it finds "nothing" between two delimiters? (It seams that it is not NULL).
I need to insert a character between two deliniters, but cannot find any if-condition for this case.
Thank you.
if strtok() finds nothing that generally means that the 2 delim chars occupy consecutive memory locations. Read up to first delim and place in an array or something. Then strcat() what you want onto the end. Then finally strcat() again onto that the end of the original string. Always make sure your destination array is big enough to cope with the 2 strcat operations without overflowing.
Simple to do with the funcs in string.h and/or pointers.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
strtok() should return NULL if it does not find the key in a string.
Kuphryn
This is what I already know :
strtok() returns a pointer to a string that includes the delimiter AND replaces the delimiter by NULL.
That means: in case there are two delimiters in a row (like ;;) strtok() should return NULL. In fact it only does so at the input-string's end!
The Output looks like this: S1 0 1Code:/* strtok example */ #include <stdio.h> #include <string.h> int main () { char str[] ="S1;;;;0;;1;;;"; char * pch; cout << "Splitting string in tokens:" << endl; pch = strtok (str,";"); while (pch != NULL) { cout << pch << " "; pch = strtok (NULL, ";"); } return 0; }
If strtok() had returned NULL when it found two ';' in a row, "0 1" would be missing.
I need to fill in 0 between each ';'.
The output has to look like: S1 0 0 0 0 0 1 0 0
@ Stoned_Coder:
Surely I can append any string at EACH token I get with strcat(). But how do I append only at the tokens with "nothing" in it? I only see that my output will look like this which is not what I want:
S10 0 0 0 00 0 10 0 0
Did I get it wrong?
Thank You.
Last edited by Zwen; 09-29-2002 at 12:40 PM.
This is just one of the many reasons I have strtok(). However, as much as I hate strtok() you can work with it. You should always do some sort of preprocessing of text that you are tokenizing. You should count the number of delimeters and get take care of crap like two consecutive delimeters.