Re: strtok
gyan wrote:
Hi
How strtok track through string?
char *strtok(char *s1, const char *s2);
As i know
The first call (with pointer s1 specified) returns a pointer to the
first character of the first token, and will have written a null character
into s1 immediately following the returned token. The function keeps track
of its position in the string between separate calls, so that
subsequent calls (which must be made with the first argument being a
null pointer) will work through the string s1 immediately following
that token.
Now suppose i have
char *ptr = "Hello:world";
char *str = "Drop:me";
printf("\n%s",strtok(ptr,":"));
printf("\n%s",strtok(str,":"));
above 2 lines will give output as
Hello
Drop
Now if i want to call strtok to get next part of string "ptr", what i
should do?since calling
printf("\n%s",strtok(NULL,":"))
will give 2nd part of str and not of ptr.
See Richard Heathfield's response for a warning about
a serious error in this code.
As to "How do I use strtok() on one string, interrupt
it to use strtok() on another, and then resume processing
the first," the answer is that you don't. strtok() uses
its own internal storage to keep track of how far it's
progressed through your string, and it can keep track of
only one position. That is a weakness in the design of
the function; there are others.
Some C implementations may provide the non-Standard
strtok_r() function that avoids this particular drawback
of strtok() -- while perpetuating the others. Perhaps it
will suit your needs. If it isn't available or if its
other weaknesses make trouble for you, C has a fairly rich
set of low-level string-bashing functions like strchr() and
strcspn() from which you can probably build whatever more
advanced capabilities you need.
--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxx
.
Relevant Pages
- Re: strtok ( ) help
... > splitCommandssomehow modifying the pointer, but I HAVE to call that ... Here's an idea of how to use the strtok() function. ... don't mind trashing the contents of a string s, ... will give you a loop that extracts the tokens one at a time from s. ... (comp.lang.c) - Re: strtok ( ) help
... >>> string between calls to strtok() its copy would no longer match. ... >> operation changed the original string between calls to strtokits ... > has to have a pointer into that string in all cases. ... (comp.lang.c) - Re: char* argv[]
... treating argv as a pointer to the first character in a string. ... terminologies. ... (comp.lang.c) - Re: char* argv[]
... argv as a pointer to the first character in a string. ... (comp.lang.c) - Re: Is this code reasonable?
... strtok() may modify the original character array path pointed to? ... path is a pointer, not the string itself. ... (comp.lang.c) |
|