I am trying to design a program in which I will create a 3 functions that resemble functions in the c-standard library (strlen,strcmp,strcpy). The first two I have gotten close to finishing, only the last one is the main problem. I am trying to create a function that will have the same functionality as the standard function strcpy. Here is what I have so far.
void myStrncpy(char destination[], const char source[], int count) {
for (int i = 0 ; source[i] != '\0' ; i++) {
count++;
}
}
So far I have gotten the length of "source" and stored it in "count". What is the next step I need to take? I would prefer to use another for loop and if statements if possible. Thanks!
****EDIT****
This is what I have now...
void myStrncpy(char destination[], const char source[], int count) {
for (int i = 0 ; source[i] != '\0' && destination[i] != '\0' ; i++) {
destination[i] = source[i];
count++;
}
}
OUTPUT:
str1 before: stringone
str2 before: stringtwo
str1 after : stringtwo
str2 after : string two
2ND RUN (WHERE I'M GETTING MY PROBLEM):
str1 before: stringthree
str2 before: stringfour
str1 after: stringfoure
str2 after: stringfour
What else do I need to put in my code so that it copies every letter until it runs out of room, or it copies every letter until it runs out of letters to copy?
If you've already written a MyStrcpy, you're almost done:
The stpncpy() and strncpy() functions copy at most n characters from s2 into s1. If s2 is less than n characters long, the remainder of s1 is filled with `\0' characters. Otherwise, s1 is not terminated.
So you have a copy of strcpy that will stop after it has copied n characters; if it stopped earlier than that (b/c you got to the end of s2), fill the rest of s1 w/ /0's.