tokenizer

C

compboy

Does anyone know how to break string apart using c?
I know that we can use strtok to tokenize a string but by using that
we can only use 1 char as a delimiter.
like:
char delimiter[] = "*";
strtok(string, delimiter);

so how do you break string aparts if the delimiter is a string that
more than 1 char length?

likes delimiter[] = "***";

eg:
string = "this is the 1st string *** this is the 2nd string"
and break it into
string1 = "this is the 1st string"
string2 = "this is the 2nd string"

Thanks a lot.
 
B

Ben Pfaff

compboy said:
Does anyone know how to break string apart using c?
I know that we can use strtok to tokenize a string but by using that
we can only use 1 char as a delimiter.
like:
char delimiter[] = "*";
strtok(string, delimiter);

so how do you break string aparts if the delimiter is a string that
more than 1 char length?

You write a function to do it for you. You are allowed to
examine and modify strings yourself, you know; there is no
restriction that only standard library functions can do so.
 
R

Richard Heathfield

compboy said:
Does anyone know how to break string apart using c?
I know that we can use strtok to tokenize a string but by using that
we can only use 1 char as a delimiter.
like:
char delimiter[] = "*";
strtok(string, delimiter);

so how do you break string aparts if the delimiter is a string that
more than 1 char length?

likes delimiter[] = "***";

eg:
string = "this is the 1st string *** this is the 2nd string"
and break it into
string1 = "this is the 1st string"
string2 = "this is the 2nd string"

Using strstr, get a pointer to the beginning of the delimiting string.

Using memcpy, copy that away to your first array. Don't forget to
null-terminate it.

Skip forward from your pointer by the length of your delimiter. You are now
pointing at the beginning of the second token.
 
C

compboy

Cool I have got it now.
thanks a lot for your answer

but I think they should add this funtion to c library.
what do you guys think?

thanks
 
C

CBFalconer

compboy said:
Cool I have got it now.
thanks a lot for your answer

but I think they should add this funtion to c library.
what do you guys think?

You have what? What answer? What "funtion"? Think about what?

Include adequate context. Google is not usenet. See below.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,226
Latest member
KristanTal

Latest Threads

Top