C语言实现字符串复制函数 - 代码示例
以下是一个简单的复制函数代码:
#include <stdio.h>
#include <string.h>
void copy(char *src, char *dest, int n){
int i;
for(i = 0; i < n; i++){
dest[i] = src[i];
}
dest[i] = '\0';
}
int main(){
char str1[] = 'Hello World!';
char str2[20];
copy(str1, str2, strlen(str1));
printf('str2: %s', str2);
return 0;
}
这个复制函数接受三个参数:源字符串、目标字符串和要复制的字符数。它使用一个循环来逐个复制源字符串中的字符到目标字符串中,最后在目标字符串的结尾添加一个 null 字符。在 main 函数中,我们使用这个函数将一个字符串复制到另一个字符串,并打印出结果。
原文地址: https://www.cveoy.top/t/topic/nfBU 著作权归作者所有。请勿转载和采集!