Hi experts,

i have this line of string: a1,b2,c3,a2,b1,c2,a3,b2,c4,

from this line,i want to load into a string array like this:
array[0]=a1,b2,c3,
array[1]=a2,b1,c2,
array[3]=a3,b2,c4,

seriously i have no idea at all.ive done sumthing but looks like rubbish.i really hope dat u experts can help me.

thanks for advance

void seeding()
{
   char seed_array[100];//line of characters in this array
   printf ("seed real=> %s\n",seed);
   char *p=NULL;

   for (int i=0;i<= 100;i++)
   {
       p=strchr(seed,',');
       if (atoi(p)==3)
       {
        strcpy (seed_array[i],p);
        *p='\n';
       }
       printf ("seed array=> %s\n",seed_array[i]);
   }

   printf("seeding test is running!!!!\n");
 }

That seed_array is declared wrong -- you need a 2d array of characters. such as char seed_array[20][200]; Instead of using strchr() it might be easier to use brute-force method

That seed_array is declared wrong -- you need a 2d array of characters. such as char seed_array[20][200]; Instead of using strchr() it might be easier to use brute-force method

what is at dt brute-force.if u dont mind

First you need a 2d array to hold the final strings. Then a row counter, a column counter, and a comma counter. In a for loop copy each character in the original array into the current row/column of the 2d array. When the 3d comma is found, increment the row counter to start a new row in the 2d array, zero out the column counter, and zero out the comma counter.

char string_array[20][80] = {0};
    char seed_array[] = "a1,b2,c3,a2,b1,c2,a3,b2,c4,";
    printf ("seed real=> %s\n",seed_array);
    char *p=NULL;
    int i,j,k;
    int comma_count = 0;

    for(i = 0, j = 0,k = 0; seed_array[i] != '\0'; ++i)
    {
        string_array[j][k++] = seed_array[i];
        if( seed_array[i] == ',' )
        {
            ++comma_count;
            if(comma_count == 3)
            {
                ++j;
                k = 0;
                comma_count = 0;
            }
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.