C语言实例-字符串复制
栏目分类:C语言教程 发布日期:2022-07-20 浏览次数:次
C 语言实例
将一个变量的字符串复制到另外一个变量中。
实例 - 使用 strcpy()
#include <stdio.h>#include <string.h>
int main(){
char src[40];
char dest[100];
memset(dest, '\0', sizeof(dest));
strcpy(src, "This is itjx.com");
strcpy(dest, src);
printf("最终的目标字符串: %s\n", dest);
return(0);
}
输出结果为:
最终的目标字符串: This is itjx.com
实例 - 不使用 strcpy()
#include <stdio.h>
int main(){
char s1[100], s2[100], i;
printf("字符串 s1: ");
scanf("%s",s1);
for(i = 0; s1[i] != '\0'; ++i)
{
s2[i] = s1[i];
}
s2[i] = '\0';
printf("字符串 s2: %s", s2);
return 0;
}
输出结果为:
字符串 s1: itjx 字符串 s2: itjx
本文由IT教学网整理发布,转载请注明出处:http://www.clang.cc//jiaocheng/cyuyan/1164.html
