storing delimited string in an array

G

gh

Hi,
I have a string variable which contains n number of comma delimited
elements and I would like to store each element into an array but I
could not figure how to do it.

for example, 1stitem,2nditem,3rditem,4thitem, ..., nthitem

What would be the best way of storing the above string into an array
like

tempary[0]="1stitem"
tempary[1]="2nditem"
tempary[2]="3rditem"
tempary[3]="4thitem"
....
....
tempary[n-1]="nthitem"

thanks.
 
V

void * clvrmnky()

gh said:
I have a string variable which contains n number of comma delimited
elements and I would like to store each element into an array but I
could not figure how to do it.

for example, 1stitem,2nditem,3rditem,4thitem, ..., nthitem

What would be the best way of storing the above string into an array
like

tempary[0]="1stitem"
tempary[1]="2nditem"
tempary[2]="3rditem"
tempary[3]="4thitem"
...
...
tempary[n-1]="nthitem"

You don't say if these are static values, or you want a general purpose
set of mutable strings.

I typically build a array of pointers to some char arrays (or char
pointers) of sufficient size (determined at runtime or compile time --
your choice depending on your application).

For example, I will often declare a char **, allocated space for the
number of char * items I want to store in it (adding room for one if I
want the notion of the last one in the "list" being null as a marker)
and then allocate room for each string as needed, associating them with
the container pointer "array".

I'm hand-waving here, but think of what "**argv" or "*argv[]" means.
 
C

CBFalconer

gh said:
I have a string variable which contains n number of comma delimited
elements and I would like to store each element into an array but I
could not figure how to do it.

for example, 1stitem,2nditem,3rditem,4thitem, ..., nthitem

What would be the best way of storing the above string into an array
like

tempary[0]="1stitem"
tempary[1]="2nditem"
tempary[2]="3rditem"
tempary[3]="4thitem"
...
...
tempary[n-1]="nthitem"

Ideally suited for the toksplit() function I published here a short
time ago. But read and heed my sig. 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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top