473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

use of strtok( )

I need a function which returns me a "word" from a given string and
then sets the pointer to the next one which is then retrieved during
further calls to the function.

I think strtok( ) is the solution but i could not understand the use of
the function as given in the C99 standard

EXAMPLE
#include <string.h>
static char str[] = "?a???b,,,#c";
char *t;
t = strtok(str, "?"); // t points to the token "a"
t = strtok(NULL, ","); // t points to the token "??b"
t = strtok(NULL, "#,"); // t points to the token "c"
t = strtok(NULL, "?"); // t is a null pointer

suppose i have a string " The C Programming Language"
how do i use strtok( ) to retrieve one word at a time from the string i.e
1st call :The
2nd call :C
3rd call :Programming
4th call :Language

Can anyone please help me out?
Apr 22 '07 #1
5 25783
Kelly B said:

<snip>
>
suppose i have a string " The C Programming Language"
how do i use strtok( ) to retrieve one word at a time from the string
i.e
1st call :The
2nd call :C
3rd call :Programming
4th call :Language

Can anyone please help me out?
#include <stdio.h>
#include <string.h>

int main(void)
{
char writablestring[] = "The C Programming Language";
char *token = strtok(writablestring, " ");
while(token != NULL)
{
printf("[%s]\n", token);
token = strtok(NULL, " ");
}
return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 22 '07 #2
Kelly B wrote:
....snip...
EXAMPLE
#include <string.h>
static char str[] = "?a???b,,,#c";
char *t;
I have read the standard a number of times now but i still don't
understand the following two lines/examples
t = strtok(NULL, ","); // t points to the token "??b"
t points to "??b" ,why not "???b"
t = strtok(NULL, "#,"); // t points to the token "c"
again #, are not in the same order in the string str
then why does t point to "c"

Apr 23 '07 #3
Kelly B said:
Kelly B wrote:
...snip...
>EXAMPLE
#include <string.h>
static char str[] = "?a???b,,,#c";
char *t;

I have read the standard a number of times now but i still don't
understand the following two lines/examples
>t = strtok(NULL, ","); // t points to the token "??b"
t points to "??b" ,why not "???b"
>t = strtok(NULL, "#,"); // t points to the token "c"

again #, are not in the same order in the string str
then why does t point to "c"
The order of the delimiters is irrelevant. strtok continues to grab
token bytes from the string until it hits any one of the specified
delimiters (or, of course, the end of the string).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 23 '07 #4
Kelly B wrote:
Kelly B wrote:
...snip...
>EXAMPLE
#include <string.h>
static char str[] = "?a???b,,,#c";
char *t;

I have read the standard a number of times now but i still don't
understand the following two lines/examples
>t = strtok(NULL, ","); // t points to the token "??b"
t points to "??b" ,why not "???b"
>t = strtok(NULL, "#,"); // t points to the token "c"

again #, are not in the same order in the string str
then why does t point to "c"
You've snipped away too much of the example code, so your
question makes no sense in isolation. Looking back at the
start of the thread, it seems to me you have missed two things:

1) In order to split the original string into individual
tokens that are themselves strings, strtok() replaces the
delimiter that ends a token with a '\0' to mark the end of
a string. The original delimiter is gone, and subsequent
calls to strtok() will not find it. (As it happens, they
won't even look at the spot where it used to be.)

2) The second argument is a *set* of delimiter characters,
not a delimiting *sequence* of characters. Any character that
appears in the second argument will be skipped if found before
a token in the input, or will serve to mark the end of a token.

Lots of people disparage strtok(), but I find it a useful
function. Like everything else, you need to understand what
it does and doesn't do and then use it properly, but if you
do so there's no reason to fear it.

--
Eric Sosman
es*****@acm-dot-org.invalid
Apr 23 '07 #5
On 22 Apr, 19:08, Kelly B <kel...@invalid.comwrote:
I need a function which returns me a "word" from a given string and
then sets the pointer to the next one which is then retrieved during
further calls to the function.

I think strtok( ) is the solution but i could not understand the use of
the function as given in the C99 standard
You need to read the description more carefully, I suspect.
EXAMPLE
#include <string.h>
static char str[] = "?a???b,,,#c";
char *t;
t = strtok(str, "?"); // t points to the token "a"
This call (with a non-NULL) first argument starts a sequence of calls
to strtok.
The second argument is a list of delimiter characters - any one of
them (or any
combination from the list) is taken as delimiting a token.

As the standard says, we start by skipping over any leading set of
delimiter
characters and stop at the first non-delimiter - the letter 'a' in
this case - this
will be our return value. We then look for the next delimiter
character, replace
the first with a null and remember the location following.

So after your first call, the second '?' in the string has been
replaced with a
null character, strtok has a note of the location of the third "?" and
we return
the address of the 'a'.
t = strtok(NULL, ","); // t points to the token "??b"
On this call, as the first argument is a NULL pointer, strtok()
continues from
its saved location - the position of the 3rd '?'. As the character
here isn't in the
current delimiter list, this will be our return value. strtok then
finds the next character
in the delimiter list - the ',' following 'b' - replaces it with a
null, and remembers the
next location (that of the following ',').
t = strtok(NULL, "#,"); // t points to the token "c"
strtok picks up from the second ','. As ',' is in the delimiter list,
it skips over this and
the subsequent ','. It finds '#' which is also in the delimiter list,
so it skips that as well.
t = strtok(NULL, "?"); // t is a null pointer
Apr 23 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ram Laxman | last post by:
Hi all, I have written the following code: /* strtok example */ #include <stdio.h> #include <string.h> static const char * const resultFileName = "param.txt";
13
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't...
20
by: bubunia2000 | last post by:
Hi all, I heard that strtok is not thread safe. So I want to write a sample program which will tokenize string without using strtok. Can I get a sample source code for the same. For exp:...
8
by: hu | last post by:
hi, everybody! I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0. Program is simple, but mistake is confusing. First, the below code can get right outcome:"ello world, hello...
4
by: Michael | last post by:
Hi, I have a proble I don't understand when using strtok(). It seems that if I make a call to strtok(), then make a call to another function that also makes use of strtok(), the original call is...
3
by: nomad5000 | last post by:
Hi everybody! I'm having trouble using strtok to fill a matrix with int nrs. from a file. the code that is not working is the following: #include <iostream> #include <fstream> #include...
29
by: Pietro Cerutti | last post by:
Hello, here I have a strange problem with a real simple strtok example. The program is as follows: ### BEGIN STRTOK ### #include <string.h> #include <stdio.h>
11
by: Lothar Behrens | last post by:
Hi, I have selected strtok to be used in my string replacement function. But I lost the last token, if there is one. This string would be replaced select "name", "vorname", "userid",...
11
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
12
by: Pilcrow | last post by:
Here is a quick program, together with its output, that illustrates what I consider to be a deficiency of the standard function strtok from <string.h>: I am using C:\>gcc --version gcc (GCC)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.