赞
踩
1.strncpy:从源字符串中拷贝n个字符到目标字符串中
- char* My_strncpy(char *dest, const char *src, int sz)
- {
- assert(dest &&src);
-
- char *start = dest;
- int i = 0;
-
- for (i = 0; i < sz; i++)
- {
- *dest = *src;
-
- if (*(dest-1) == '\0')
- {
- *dest = '\0';
- }
- dest++;
- src++;
- }
-
- return start;
- }
注意:strcnpy中若n大于源字符串的字符数,则剩余的部分用‘\0'补充。
2.strncat:在目标字符串后追加源字符串中n个字符。
- char* My_strncat(char *dest, const char *src, int sz)
- {
- assert(dest && src);
-
- char *start = dest;
- int i = 0;
-
- while (*dest != '\0')
- {
- dest++;
- }
-
- for (i = 0; i < sz; i++)
- {
- *dest++ = *src++;
- }
-
- if (*(src-1) != '\0')
- {
- *dest = '\0';
- }
-
- return start;
- }
- <
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。