Need some help with arrays

How would I go about copying a set of array characters alternatively to a new array?
For example, I take "abcdef", split them and then copy them alternatively to a new array that would contain "adcedf".
closed account (zb0S216C)
There are two ways:

1) Use std::memcpy()[1]:

1
2
3
4
char main_str[4] = "AWH";
char new_str[4] = {'\0'};

std::memcpy(new_str, main_str, (4 * sizeof(char)));


2) Use a for loop to transfer each character accordingly:

1
2
for(short offs(0); offs < 4; ++offs)
    new_str[offs] = main_str[offs];

Take your pick.

References:
[1] http://www.cplusplus.com/reference/clibrary/cstring/memcpy/


Wazzak
Last edited on
I know how to copy them straight forward from 0 to 4 but
I need to do this with an array like this char word[9] = "abcdefgh"
I took half of them so I have "abcd" and "efgh" now I want to copy them alternatively to a new array

so I take the "a" from the first half, "e" from the second half and I get "ae"
then so I take the "b" from the first half, "f" from the second half and I get "bf"
so on and so forth until I get
"aebfcgdh"

I just need to know how am I able to copy them alternatively
You can do it 'in place':

The first element of the second array will be the first of the first. The second of the second will be the size/2'th element of the first. The third of the second will be the second of the first, etc.

That's a very easy pattern to translate into code!
Topic archived. No new replies allowed.