strtok

G

gyan

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.
 
R

Richard Heathfield

gyan said:
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,":"));

Stop right there. strtok modifies the string you pass to it. Here, you are
passing it the start address of a string literal. Use an array instead:

char haystack[] = "Hello:world:eek:f:fun:and:games:with:C";
char *token;
for(token = strtok(haystack, ":"); token != NULL; token = strtok(NULL, ":"))
{
printf("%s\n", token);
}
 
E

Eric Sosman

gyan said:
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.
 
B

Barry Schwarz

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.

After you fix the problem with passing the address of a string literal
as the first argument, you could do something like
ptr = strtok(ptr+strlen(ptr)+1,":");
but you will have to do your own checking for when you reach the end
of your original string.



<<Remove the del for email>>
 
R

reyescar11

The strtok modifies the original string so you must first create a
duplicate of that string or copy it to another string, so the changes
apply to the another string and not the original; do this if u want to
keep your original string as it was.

Another point to say is that the strtok drops you the charachters after
and before that a NULL charachter was found, example: "Hello;my;friend"
in this case if your NULL char is ; the strings dropped by the strtok
function should be:
1. Hello
2. my
3. friend

remember save the original string in another string and use the strtok
function to get the tokens from the secondary string so you wont lose
any data from the original String
 
M

Mark McIntyre

On 5 Dec 2005 15:31:50 -0800, in comp.lang.c , (e-mail address removed)
wrote:
(contextless remarks)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top