implement the strncpy function

How do you implement the strncpy function of the standard library?
Well we start with the header:
1
2
3
void stringCopy(char *dest, char *src, int length) {
  ...
}


Do you have that so far, or can/did you give this a try?
Do you know what it does? Start with the function prototype and consider how to do that.
yes I have that but do not know where to go from that
Ok so if you had some sample input say:
1
2
3
4
5
char strA[] = "Hello."; //{ 'H', 'e', 'l', 'l', 'o', '.', '\0' }, size = 7
char *strB = new char[10]; //can hold up to a 9 character string (not including the ending '\0')
size = 10; //copy up to the first 10 characters, strB can't hold more then that!

stringCopy( strB, strA, size );


Think about what you want it to do with the array stored at strA.
Think about where you want it to stop.
Try it with char strA[] = "Hello, world."; and then think about where you want it to stop.
Hint: You will likely need a loop for this function.
Last edited on
Topic archived. No new replies allowed.