char text[21] //contains a 0x00 terminated string between 5..20 characters
char textA[11] //is expected to contain the first 10 chars
char textB[11] //is expected to contain the second 10 chars
// so if text is "0123456789ABCD" , then textA should be "0123456789"
//text B should be "ABCD " (10 characters)
strncpy (textA, text, 11); //should fill the first string , but if text is shorter than 10, it won't be padded with extra space characters.
how to fill the second one , (from offset 10 to 20) , I don't know
All I can think of is a for loop that copies one character at a time. (then it would be easy to overwrite the remaining characters with space or 0x0 too)
All I can think of is a for loop that copies one character at a time. (then it would be easy to overwrite the remaining characters with space or 0x0 too)
char text[21] //contains a 0x00 terminated string between 5..20 characters
char textA[11] //is expected to contain the first 10 chars
char textB[11] //is expected to contain the second 10 chars
char tmp[11];
strcpy(text, "0123456789ABCDEF");
// fill tmp with 0x00
memset(tmp, 0, sizeof(tmp);
// copy first 10 characters (or less) to tmp
strncpy(tmp, text, 10);
// pad
snprintf(textA, sizeof(textA), "%-10s", tmp);
// fill tmp with 0x00
memset(tmp, 0, sizeof(tmp);
// copy second 10 characters (or less) to tmp
strncpy(tmp, &text[10], 10);
// pad
snprintf(textB, sizeof(textB), "%-10s", tmp);
Looping might be cheaper from a memory usage perspective.