Click here to Skip to main content
15,947,389 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a buffer as below,

Buff[] = "3,501,sjdc,34567,,502,hsdhbd,45678,5678,503,sdnns,5678,678"

3 is the No of Fields and each field has 4 parameters

Like :
Field 1 : 501,sjdc,34567,
Field 2 : 502,hsdhbd,45678,5678
Field 3 : 503,sdnns,5678,678 -> Final Result of case 1

So my number of fields in the Buffer is a generic values and respective fields will attached to the string.

Now i need to split the string accordingly and should be able to manipulate as below:

In Field 1 last parameter is empty so i should be able to modify it with tmpvalue and my Final Buffer should be

Buff[] = "3,501,sjdc,34567,7878,502,hsdhbd,45678,5678,503,sdnns,5678,678"

Note: Any Parameter of the field can be empty and should be able to modify it in a generic way

Please help me in this?

What I have tried:

I have tried by splitting the string but not able to get the empty parameter properly and not able to modify in between.
Posted

Firstly, you need to work out / define what makes an "empty parameter" get a specific value - there isn't anything in the rest of that row's data that obviously connects it to "7878" - and without any idea why it needs to be that specific value you can't proceed any further. Even if it's "7878 is the code for an empty parameter" - which would be really silly - it needs to be defined as you will otherwise fill your data with actual rubbish and it'll be really difficult to fix it once you have done that.

When you have that sorted out, split your string in two parts: the rows count and the rest of the data. Convert the rows count to an integer and start processing the rest of the data.

Split out each set of four parameters delimited by their commas.

Then process each set of four as a separate row: identify empty parameters - which is easy if you split the four column data items by commas - and work out the replacement value.

Finally, reassemble the whole data complete with count and reinsert the commas to separate each value.

This isn't a difficult exercise if you take it in simple steps and make sure that each part works properly before you move on to the next.
 
Share this answer
 
You need to split the string manually, since strtok does not return empty tokens. Using strchr to find each comma, you can do something like:
C++
int count = atoi(Buff); // get the count from the first field
next = strchr(Buff, ',') + 1;
while (count-- > 0)     // repeat for count sets
{
    for (int i = 0; i < 4; ++i) // extract the four fields for each set
    {
        fields[i] = next;         // save the pointer to the next field
        next = strchr(next, ','); // find the end of the token
        if (next == NULL)         // if the pointer is null we have reached the end of the text
            break;
        *next = '\0';             // add the null terminator
        next++;                   // and point to the next field
    }
    for (int i = 0; i < 4; ++i)
    {
        printf("%s, ", fields[i]);
    }
    printf("\n");
}
 
Share this answer
 
Comments
merano99 11-May-24 15:18pm    
+5, looks good
To complete Richard's solution, you could also use the tmpvalue, as requested.
C
// ...
char* tmpvalue = "7878";
char* next = strchr(Buff, ',') + 1;
while (count-- > 0) {
	// ...
	*next = '\0';             // add the null terminator
	if (strlen(fields[i]) == 0)
		fields[i] = tmpvalue;
	next++;                   // and point to the next field
}
for (int i = 0; i < 4; ++i) {
	printf("%s", fields[i]);
	if (i < 3)
		printf(", ");
}
printf("\n");
 
Share this answer
 
Comments
Richard MacCutchan 12-May-24 3:20am    
+5 for you too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900