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

using strtok to mark delimiters as tokens

hi,
i wanted to know whether we can use strtok command to mark delimiters
as tokens as well.In Java,we have a command:

StringTokennizer(String str, String delimiters, boolean
delimAsToken)

which considers the delimiters as tokens,too.Can strtok accomplish
this requirement?or could you please let me know if there is any other
command in C that would carry out this task?
Mar 12 '08 #1
5 3334
gp*****@gmail.com wrote:
hi,
i wanted to know whether we can use strtok command to mark delimiters
as tokens as well.In Java,we have a command:

StringTokennizer(String str, String delimiters, boolean
delimAsToken)

which considers the delimiters as tokens,too.Can strtok accomplish
this requirement?
You could force strtok to consider delimiters as tokens by specifying a
different delimiter string at the next call to it. But this might not
be quite what you want though.
or could you please let me know if there is any other
command in C that would carry out this task?
You can use a string function like strstr or strcspn to locate the
necessary substring and tokenise the string yourself. In fact, it may
be more robust to use these functions and write your own tokeniser, as
strtok has a number of caveats you need to be aware of to use it
safely.

Mar 12 '08 #2
gp*****@gmail.com wrote:
hi,
i wanted to know whether we can use strtok command to mark delimiters
as tokens as well.In Java,we have a command:

StringTokennizer(String str, String delimiters, boolean
delimAsToken)

which considers the delimiters as tokens,too.Can strtok accomplish
this requirement?
No, it can't.
or could you please let me know if there is any other
command in C that would carry out this task?
You could use one of the techniques others have already suggested, and
write your own.

Or you could do what's recommended with later versions of Java and move
to using a regular expression parser to pick apart the string.
Mar 12 '08 #3
santosh wrote:
gp*****@gmail.com wrote:
hi,
i wanted to know whether we can use strtok command to mark
delimiters as tokens as well.In Java,we have a command:

StringTokennizer(String str, String delimiters, boolean
delimAsToken)

which considers the delimiters as tokens,too.Can strtok accomplish
this requirement?

You could force strtok to consider delimiters as tokens by specifying
a different delimiter string at the next call to it. But this might
not be quite what you want though.
Not really. strtok() punches a hole in the string where that first
delimiter was. No way to save it. Besides, presumably the OP wants to
keep using the delimiter set.

I'm not sure what the exact semantics of the Java call are, it would
have to be specified better. What I would do would be an approach more
like PHP explode(), and produce an array or list of strings. Then it
would be relatively easy to save the delimiters too.


Brian
Mar 12 '08 #4
<gp*****@gmail.comwrote in message
>
Can strtok accomplish
this requirement?or could you please let me know if there is any other
command in C that would carry out this task?
sscanf() might be what you are looking for.

Alternatively, if you are implementing something like a BASIC interpreter,
you need a struct with the string, the read position, and the top token
read. Then you have a match() function to get rid of the current token and
get a new one, triggering an error if there is no match (for instance for
closing paretheses].
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Mar 12 '08 #5
On Wed, 12 Mar 2008 10:42:28 +0000, Richard Bos wrote:
Falcon Kirtaran <fa********@iridiumlinux.orgwrote:
>gp*****@gmail.com wrote:
i wanted to know whether we can use strtok command to mark delimiters
as tokens as well.In Java,we have a command:
>I don't think there is. This is because the function is generally
implemented by replacing the delimiter with a null.

s/generally/required to be/.
>It'd be really easy to write your own, however, that preserves the
delimiting character. For instance, it might look something like:

char * strtok_d(char * str, const char * tokens, char * delim) {

Don't call it that, though. Names starting in str and a letter are
reserved for the implementation's use.
Strictly speaking, this is not true.

The standard states that function names that begin with str and a
*lowercase* letter may be reserved. Thus, for example, a
function name such as stricmp is reserved, whereas a function name such
as strIcmp is not reserved--the user is free to define such a function.

In my opinion, if you want to both define a function name that begins
with "str" and at the same time avoid using a reserved identifier, then
use a name that begins with "str_", i.e., str_icmp.

If this seems overly pedantic, then there's a good chance I'd agree with
you.

For example, the function name strip_leading_white_space will never, ever
conflict with a function name defined by an implementation (the
implementation would in all probability, if it in fact defined such a
function, use something such as stripleadingwhitespace or striplws or
strlws or some other name).

The probability that an identifier you define conflicts with an
identifier defined by the implementation decreases exponentially,
multiplied by some constant, with the number of underscores used in your
identifier.

In my experience, a single underscore is all that is needed, even if your
identifier does begin with "str" and (followed by) a lowercase letter.

--
jay
Mar 13 '08 #6

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

Similar topics

1
by: arssa2020 | last post by:
hello, i am writing a GPS app in evc++ 4.0, i am using c-style char* instead of CStrings, i want to parse NMEA sentence such as "GPZDA,162913.48,19,08,2005,,*63" problem is when i use strtok it...
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: manochavishal | last post by:
Hi I am writing a Program in which i get input as #C1012,S,A#C1013,S,U I want to get C1012,S,A using strtok and then pass this to function CreateVideo which will further strtok this...
33
by: Geometer | last post by:
Hello, and good whatever daytime is at your place.. please can somebody tell me, what the standard behavior of strtok shall be, if it encounters two or more consecutive delimiters like in...
14
by: Mr John FO Evans | last post by:
I cam across an interesting limitation to the use of strtok. I have two strings on which I want strtok to operate. However since strtok has only one memory of the residual string I must complete...
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",...
75
by: siddhu | last post by:
Dear experts, As I know strtok_r is re-entrant version of strtok. strtok_r() is called with s1(lets say) as its first parameter. Remaining tokens from s1 are obtained by calling strtok_r() with...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...

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.