Advertisement
liftchampion

test1821

Oct 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.62 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3.                             Online C Compiler.
  4.                 Code, Compile, Run and Debug C program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <stdio.h>
  10.  
  11. /*char *ft_strncpy(char *dest, char *src, unsigned int n)
  12. {
  13.     char *ptr;
  14.     unsigned int i;
  15.     char tmp;
  16.    
  17.     i = 0;
  18.     if (dest == NULL)
  19.     {
  20.         return (NULL);
  21.     }
  22.     ptr = dest;
  23.     while (*src != '\0' && i < n)
  24.     {
  25.         tmp = *(src + 1);
  26.         *dest = *src;
  27.         dest++;
  28.         src++;
  29.         i++;
  30.     }
  31.     if (*src == '\0')
  32.     {
  33.         *dest = '\0';
  34.     }
  35.     return (ptr);
  36. }*/
  37.  
  38. char *ft_strncpy(char *dest, const char *src, size_t n)
  39. {
  40.     char *ret = dest;
  41.     do {
  42.         if (!n--)
  43.             return ret;
  44.     } while (*dest++ = *src++);
  45.     while (n--)
  46.         *dest++ = 0;
  47.     return ret;
  48. }
  49.  
  50. int main()
  51. {
  52.     char src[] = "123456789";
  53.     char dest[] = "abcdefgheh";
  54.  
  55.     for (int i = 0; i <= 9; i++)
  56.     {
  57.         printf("%c(%d) ", src[i], src[i]);
  58.     }
  59.     printf("\n");
  60.     for (int i = 0; i <= 10; i++)
  61.     {
  62.         printf("%c(%d) ", dest[i], dest[i]);
  63.     }
  64.     printf("\n\n");
  65.     ft_strncpy(src + 3, src, 5u);
  66.     for (int i = 0; i <= 9; i++)
  67.     {
  68.         printf("%c(%d) ", src[i], src[i]);
  69.     }
  70.     printf("\n");
  71.     for (int i = 0; i <= 10; i++)
  72.     {
  73.         printf("%c(%d) ", dest[i], dest[i]);
  74.     }
  75.     printf("\n\nDone!");
  76.  
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement