473,385 Members | 1,531 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,385 software developers and data experts.

Parse tokens from a string


in C, is there any function can be used to decompose tokens from a string? if
not, can i find it in CPP? thanks!

--
steven woody (id: narke)

Celine: Well, who says relationships have to last forever?

- Before Sunrise (1995)
Nov 15 '05 #1
7 2637
Steven Woody wrote:
in C, is there any function can be used to decompose tokens from a string? if
not, can i find it in CPP? thanks!


The original C library comes with a function to do that, it's called
strtok, which stands for "string tokenise" IIRC. The interface leaves
something to be desired though. It's not that hard to do it manually by
manipulating the string as an array of char or through pointers.

char foo[] = "joy to the world";
char *p = strtok(foo, " ");
char *q = strtok(0, " ");
char *r = strtok(0, " ");
char *s = strtok(0, " ");

now
p == foo
q == foo + 4
r == foo + 7
s == foo + 11

and
foo[3] == 0 (previously was ' ')
foo[6] == 0 (previously was ' ')
foo[10] == 0 (previously was ' ')
foo[16] == 0 (unchanged)

Make sure you understand that it modifies the original array, and
returns pointers into that array! The strings you get back should be
copied into some more permanent storage if you plan to re-use the
original array to read in another line of input.

Also understand that it treats several consecutive separators as one, so
the string "a,b,,d" will be parsed as {"a", "b", "d"} instead of {"a",
"b", "", "d"}.

--
Simon.
Nov 15 '05 #2


Steven Woody wrote:
in C, is there any function can be used to decompose tokens from a string? if
not, can i find it in CPP? thanks!


There's strtok(). It has some drawbacks and may not do
exactly the kind of "decomposition" you want -- but since
you didn't describe what you want, all I can do is suggest
that you take a look.

Another possibility is sscanf(). It, too, has drawbacks
and may not do exactly what you had in mind, but take a
look anyhow.

Finally, there's roll-your-own string-bashing with
strchr(), strrchr(), strstr(), strspn(), strcspn(), ...

--
Er*********@sun.com

Nov 15 '05 #3
Eric Sosman wrote:
Steven Woody wrote:
in C, is there any function can be used to decompose tokens from a string? if
not, can i find it in CPP? thanks!


There's strtok(). It has some drawbacks and may not do
exactly the kind of "decomposition" you want -- but since
you didn't describe what you want, all I can do is suggest
that you take a look.


thought not standard, strsep() may also be available in some
environments. if it's not, the source is easy to come by and
incorporate.

Nov 15 '05 #4
Eric Sosman <er*********@sun.com> writes:
Steven Woody wrote:
in C, is there any function can be used to decompose tokens from a string? if
not, can i find it in CPP? thanks!


There's strtok(). It has some drawbacks and may not do
exactly the kind of "decomposition" you want -- but since
you didn't describe what you want, all I can do is suggest
that you take a look.


For what it's worth, here is my standard list of drawbacks to
strtok():

* It merges adjacent delimiters. If you use a comma as
your delimiter, then "a,,b,c" is three tokens, not
four. This is often the wrong thing to do. In fact,
it is only the right thing to do, in my experience,
when the delimiter set is limited to white space.

* The identity of the delimiter is lost, because it is
changed to a null terminator.

* It modifies the string that it tokenizes. This is bad
because it forces you to make a copy of the string if
you want to use it later. It also means that you can't
tokenize a string literal with it; this is not
necessarily something you'd want to do all the time but
it is surprising.

* It can only be used once at a time. If a sequence of
strtok() calls is ongoing and another one is started,
the state of the first one is lost. This isn't a
problem for small programs but it is easy to lose track
of such things in hierarchies of nested functions in
large programs. In other words, strtok() breaks
encapsulation.

--
"I hope, some day, to learn to read.
It seems to be even harder than writing."
--Richard Heathfield
Nov 15 '05 #5
tedu wrote:

Eric Sosman wrote:
Steven Woody wrote:
in C, is there any function
can be used to decompose tokens from a string? if
not, can i find it in CPP? thanks!


There's strtok(). It has some drawbacks and may not do
exactly the kind of "decomposition" you want -- but since
you didn't describe what you want, all I can do is suggest
that you take a look.


thought not standard, strsep() may also be available in some
environments. if it's not, the source is easy to come by and
incorporate.


Here's what I have for strsep:

#include <string.h>

char *str_sep(char **s1, const char *s2)
{
char *const p1 = *s1;

if (p1 != NULL) {
*s1 = strpbrk(p1, s2);
if (*s1 != NULL) {
*(*s1)++ = '\0';
}
}
return p1;
}
/*
** K&R2 Exercise 2-4
** alternate squeeze function using str_sep.
*/

char *str_squeeze_s(char *s1, const char *s2)
{
char *const p1 = s1;
const char *const p2 = s2;
char * p3 = p1;

do {
s2 = str_sep(&p3, p2);
while (*s2 != '\0') {
*s1++ = *s2++;
}
} while (p3 != NULL);
*s1 = '\0';
return p1;
}

--
pete
Nov 15 '05 #6
Eric Sosman <er*********@sun.com> writes:
Steven Woody wrote:
in C, is there any function can be used to decompose tokens from a string? if
not, can i find it in CPP? thanks!
There's strtok(). It has some drawbacks and may not do
exactly the kind of "decomposition" you want -- but since
you didn't describe what you want, all I can do is suggest
that you take a look.


strtok's man page say, it can not handle empty field. what exactly does this
mean ?

actually, what i want to do is:

1, read a line in a text configuration file.
2, the line are just string tokens seperated white spaces.
3, goto 1 until eof.

Another possibility is sscanf(). It, too, has drawbacks
and may not do exactly what you had in mind, but take a
look anyhow.

Finally, there's roll-your-own string-bashing with
strchr(), strrchr(), strstr(), strspn(), strcspn(), ...

--
Er*********@sun.com


--
steven woody (id: narke)

Celine: Well, who says relationships have to last forever?

- Before Sunrise (1995)
Nov 15 '05 #7
pete <pf*****@mindspring.com> writes:
tedu wrote:

Eric Sosman wrote:
> Steven Woody wrote:
> > in C, is there any function
> > can be used to decompose tokens from a string? if
> > not, can i find it in CPP? thanks!
>
> There's strtok(). It has some drawbacks and may not do
> exactly the kind of "decomposition" you want -- but since
> you didn't describe what you want, all I can do is suggest
> that you take a look.


thought not standard, strsep() may also be available in some
environments. if it's not, the source is easy to come by and
incorporate.


Here's what I have for strsep:

#include <string.h>

char *str_sep(char **s1, const char *s2)
{
char *const p1 = *s1;

if (p1 != NULL) {
*s1 = strpbrk(p1, s2);
if (*s1 != NULL) {
*(*s1)++ = '\0';
}
}
return p1;
}
/*
** K&R2 Exercise 2-4
** alternate squeeze function using str_sep.
*/

char *str_squeeze_s(char *s1, const char *s2)
{
char *const p1 = s1;
const char *const p2 = s2;
char * p3 = p1;

do {
s2 = str_sep(&p3, p2);
while (*s2 != '\0') {
*s1++ = *s2++;
}
} while (p3 != NULL);
*s1 = '\0';
return p1;
}

--
pete


thanks for sharing the code, it's worthy to read.

--
steven woody (id: narke)

Virginia Woolf: Someone has to die Leonard, in order that the rest of
us should value our life more.

- The Hours (2002)
Nov 15 '05 #8

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

Similar topics

9
by: Martoni | last post by:
I need to parse a string with an embedded email address. The string always has the format NAME (name@domain) SOMETEXT. What I need to get is the email address as name@domain. I came up with this...
2
by: N | last post by:
Hi, I would like to parse out each value that is seperated by a comma in a field and use that value to join to another table. What would be the easiest way to do so without having to write a...
15
by: Jeannie | last post by:
Hello group! I'm in Europe, traveling with my laptop, and I don't any compilers other than Borland C++ 5.5. available. I also don't have any manuals or help files available. Sadly, more...
9
by: Danny | last post by:
HI again Is there a nifty function in access that will: 1. return the amount of occurances of a small string within a larger string? this<br>is<br>a<br>test would return 3 for <br>
19
by: linzhenhua1205 | last post by:
I want to parse a string like C program parse the command line into argc & argv. I hope don't use the array the allocate a fix memory first, and don't use the memory allocate function like malloc....
16
by: Charles Law | last post by:
I have a string similar to the following: " MyString 40 "Hello world" all " It contains white space that may be spaces or tabs, or a combination, and I want to produce an array...
4
by: Phil Mc | last post by:
OK this should be bread and butter, easy to do, but I seem to be going around in circles and not getting any answer to achieving this simple task. I have numbers in string format (they are...
1
by: Dan Somdahl | last post by:
Hi, I am new to ASP but have what should be a fairly simple task that I can't figure out. I need to parse a string from a single, semi-colon delimited, 60 character field (el_text) in a recordset...
1
by: (2b|!2b)==? | last post by:
I am expecting a string of this format: "id1:param1,param2;id2:param1,param2,param3;id" The tokens are seperated by semicolon ";" However each token is really a struct of the following...
2
by: tekt9t9 | last post by:
Hello I want to parse a string which holds a integer value to an int. The Interger.parseint() throws an exception. e.g If i try to execute the following code: i =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.