char array copying
What's the best way or function to copy one pointer char array to another, but they are different sizes. I have one that is 200 bytes but I only wan t to capture the first 30 and put in a differet array. How can I do this?
edit: or is there a way to copy only till a specified delimiter, other than the NULL char?
Thanks!
# 1 Re: char array copying
You are asking several questions at once.
What's the best way...What are the rules for the contest? Speed? Brevity of code? Readability?
I only want to capture the first 30 and put in a differet arrayWhat is your language? C? C++? with or without STL? with ASCI characters or Unicode or Multibytes?
My choice in C with Ascii characters is
#include <stdio.h>
char *strncpy(char *dest, const char *src, size_t maxlen);
e.g.
strncpy(dst_array, src_array, 30);
Is there a way to copy only till a specified delimiter?
My choice in C with Ascii characters is:
int i;
for (i= 0; i < sizeof(src_array) && i < sizeof(dst_array) - 1; i++) {
if (src_array[i] == delim_char) break;
dst_array[i] = src_array[i];
}
dst_array[i] = '\0'; // I like to have null terminated stringsN.B. If this loop is placed in a function and the arrays are passed to the function by the way of their pointers, then sizeof() gives only the length of the pointers, not the length of the arrays.