Hi all,

In c,i want to split string into array.

Example:
char *string="abcdefgh";

into char arrays like 'ab','cd','ef','gh'.

Any suggestions or ideas are welcome...

You have to copy the characters you want into the new arrays. In your example you will have to declare four character arrays (not pointers!) that are 3 characters each, the 3d one is for the null terminating character. Then just iterate through the original string using either an index counter or a pointer, whichever one is easier for you.

Another way to do the actual copying is to call strncpy() to copy 2 characters from source to destination strings.

Member Avatar for MonsieurPointer

And if you are up to the challenge, use dynamic arrays. :P

You have to copy the characters you want into the new arrays. In your example you will have to declare four character arrays (not pointers!) that are 3 characters each, the 3d one is for the null terminating character.

No, only 2 characters each. He wants character arrays, not c-strings.

There is no reason to allocate space for a trailing null if you are dealing with arrays, only with strings.

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.