- #include <stdio.h>
- #include<assert.h>
- char *my_strncpy(char *dest, const char* src, int n)
- {
- assert(dest != NULL);
- assert(src != NULL);
- char*ret = dest;
- while (n) //复制n个字符,循环n次
- {
- *dest++ = *src++;
- n--;
- }
- if (*(dest- 1) != '\0')//判断是否已经将‘\0’复制到目标字符串中
- *dest = '\0'; //若没有则给目标字符串最后添加‘\0’
- return ret;
- }
- int main()
- {
- char*p = "abcd";
- char q[5];
- printf("%s\n", my_strncpy(q, p,2));
- system("pause");
- return 0;
- }
模拟实现strcpy
最新推荐文章于 2024-04-21 16:27:54 发布