Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strtok

Reply
Thread Tools

strtok

 
 
gyan
Guest
Posts: n/a
 
      11-25-2005
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.

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      11-25-2005
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:worldf:fun:and:games:with:C";
char *token;
for(token = strtok(haystack, ":"); token != NULL; token = strtok(NULL, ":"))
{
printf("%s\n", token);
}


--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      11-25-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      11-25-2005
On Fri, 25 Nov 2005 01:29:29 -0500, "gyan" <(E-Mail Removed)> 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.


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>>
 
Reply With Quote
 
reyescar11@hotmail.com
Guest
Posts: n/a
 
      12-05-2005
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

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      12-05-2005
On 5 Dec 2005 15:31:50 -0800, in comp.lang.c , (E-Mail Removed)
wrote:
(contextless remarks)
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      12-05-2005
(E-Mail Removed) wrote:

> NULL charachter


You misspelled "null" and "character".

--
pete
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strtok() and std::string Alex Vinokur C++ 6 04-14-2005 01:40 PM
Problems with strtok() returning one too many tokens... Adam Balgach C++ 2 11-28-2004 01:12 AM
segfault on strtok Fatih Gey C Programming 40 11-01-2003 07:24 PM
strtok trouble Robert C Programming 17 09-06-2003 10:30 PM
strtok problem jorntk@yahoo.com C Programming 4 08-29-2003 11:26 AM



Advertisments