wrote:
> hi,
> i wanted to know whether we can use strtok command to mark delimiters
> as tokens as well.In Java,we have a command:
>
> StringTokennizer(String str, String delimiters, boolean
> delimAsToken)
>
> which considers the delimiters as tokens,too.Can strtok accomplish
> this requirement?or could you please let me know if there is any other
> command in C that would carry out this task?
I don't think there is. This is because the function is generally
implemented by replacing the delimiter with a null.
It'd be really easy to write your own, however, that preserves the
delimiting character. For instance, it might look something like:
char * strtok_d(char * str, const char * tokens, char * delim) {
int i1 = 0;
int i2 = 0;
static char * istr;
if (str) istr = str; /*if str is non-null, use str instead of the*/
/*stored value*/
while (istr[i1]) {
while (tokens[i2]) {
if (istr[i1] == tokens[i2]) {
*delim = tokens[i2]; /*save the token*/
tokens[i2] = 0; /*tokenize*/
istr += i2; /*set a new istr for the next call*/
return istr;
};
};
i2 = 0; /*re-init after use*/
i1++; /*iterate through*/
};
return 0; /*should not arrive here*/
};
The only thing you'd need to take care of is that both parameters must
be null-terminated strings. Also, you'd have to check if the third
parameter when dereferenced is not 0. Obviously, the third parameter is
just a single char (by reference), not a string.
The function could return it as a char *, but that would massively
modify the behaviour of the function, and would probably require it to
allocate a char array that you'd later have to free(). Calls to this
can't be mixed interchangeably with calls to strtok().
I've not tested this function at all; it's purely theoretical
--
--Falcon Kirtaran