I have to rewrite for an assignment a function that mimics the behavior of the strncpy, after many trials and error, some external help, here's the final code :
 15 char    *ft_strncpy(char *dest, char *src, unsigned int n)
 16 {
 17     unsigned int i;
 18     unsigned int size;
 19
 20     i = 0;
 21     size = 0;
 22     while (src[i] && i < n)
 23     {
 24         dest[i] = src[i];
 25         i++;
 26     }
 27     while (i < n)
 28     {
 29         dest[i] = '\0';
 30         i++;
 31     }
 32     return (dest);
 33 }
It works flawlessly, but I don't get this part :
 while (i < n)
 {
     dest[i] = '\0';
     i++;
 }
At this point, the value of i should be (n - 1) right ? So '\0' goes into dest[n-1] and the loop ends because i becomes equal to n and then the function ends.
We're left with a string that would look something like this :
"string copied\0not copied part"
And be printed like : string copiednot copied part.
My question is :
- Why does - dest[n-1]='\0'or- dest[n]='\0'instead of that while loop, return- string copiedinstead of 'string copiednot copied part' when they basically do the same thing ?
- Why does the - \0seem to be 'ignored' when printed out after the while loop, when it's considered a full stop when I use dest[n-1] = '\0' instead ?
Here's the main/alternative function I used to run test and try to understand :
int main()
{
     char str[] = "test de chaine";
     char *str2 = "chaine de test";
     ft_strncpy(str, str2, 6);
     printf("%s", str);
     return 0;
}
char    *ft_strncpy(char *dest, char *src, unsigned int n)
 {
     unsigned int i;
     unsigned int size;
     i = 0;
     size = 0;
     while (src[i] && i < n)
     {
         dest[i] = src[i];
         i++;
     }
         dest[n-1] = '\0';
     return (dest);
 }
 
     
     
    