Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Online C Compiler.
- Code, Compile, Run and Debug C program online.
- Write your code in this editor and press "Run" button to compile and execute it.
- *******************************************************************************/
- #include <stdio.h>
- /*char *ft_strncpy(char *dest, char *src, unsigned int n)
- {
- char *ptr;
- unsigned int i;
- char tmp;
- i = 0;
- if (dest == NULL)
- {
- return (NULL);
- }
- ptr = dest;
- while (*src != '\0' && i < n)
- {
- tmp = *(src + 1);
- *dest = *src;
- dest++;
- src++;
- i++;
- }
- if (*src == '\0')
- {
- *dest = '\0';
- }
- return (ptr);
- }*/
- char *ft_strncpy(char *dest, const char *src, size_t n)
- {
- char *ret = dest;
- do {
- if (!n--)
- return ret;
- } while (*dest++ = *src++);
- while (n--)
- *dest++ = 0;
- return ret;
- }
- int main()
- {
- char src[] = "123456789";
- char dest[] = "abcdefgheh";
- for (int i = 0; i <= 9; i++)
- {
- printf("%c(%d) ", src[i], src[i]);
- }
- printf("\n");
- for (int i = 0; i <= 10; i++)
- {
- printf("%c(%d) ", dest[i], dest[i]);
- }
- printf("\n\n");
- ft_strncpy(src + 3, src, 5u);
- for (int i = 0; i <= 9; i++)
- {
- printf("%c(%d) ", src[i], src[i]);
- }
- printf("\n");
- for (int i = 0; i <= 10; i++)
- {
- printf("%c(%d) ", dest[i], dest[i]);
- }
- printf("\n\nDone!");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement