0

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...

4
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by WaltP
0

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.

Edited by Ancient Dragon: n/a

0

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.

Edited by WaltP: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.