strncat函数模拟实现
strncat函数在cplusplus网站中的定义
模拟实现源码
//strncat函数模拟实现 #include <stdio.h> #include <string.h> #include <assert.h> char* my_strncat(char* destination, const char* source, size_t num) { assert(destination && source); char* ret = destination; while (*destination) { destination++; } while (num--) { *destination = *source; destination++; source++; } return ret; } int main() { char arr1[] = "efgh"; char arr2[20] = "abcd"; printf("arr2:%s\n", arr2); char* ret = my_strncat(arr2, arr1, 3); printf("arr1:%s\n", arr1); printf("arr2:%s\n", arr2); return 0; }
执行结果