C 语言实例 - 字符串复制

将一个变量的字符串复制到另外一个变量中。

实例 - 使用 strcpy()

c 复制代码
#include <stdio.h>
#include <string.h>
 
int main()
{
   char src[40];
   char dest[100];
  
   memset(dest, '\0', sizeof(dest));
   strcpy(src, "This is runoob.com");
   strcpy(dest, src);
 
   printf("最终的目标字符串: %s\n", dest);
   
   return(0);
}

输出结果为:

c 复制代码
最终的目标字符串: This is runoob.com

实例 - 不使用 strcpy()

c 复制代码
#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;
}

输出结果为:

c 复制代码
字符串 s1: runoob
字符串 s2: runoob
相关推荐
Chester_19997 小时前
CSP202203C.计算资源调度器
开发语言·数据结构·c++·蓝桥杯
EXI-小洲8 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
会周易的程序员8 小时前
给 PLC 写一个字节码虚拟机:STVM 虚拟机架构设计
开发语言·c++·虚拟机·软plc·iec61131·stvm
机器视觉知识推荐、就业指导8 小时前
为什么老说“纯 Qt 没啥就业市场”?
开发语言·qt
telepan8 小时前
Qt 开发避坑与性能指南:如何优雅、安全地定义全局常量字符串?
开发语言·qt
m0_695251499 小时前
Qt MaintenanceTool 使用国内镜像加速下载(超详细教程)
开发语言·qt
是隼人9 小时前
buuctf-pwn xdctf2015_pwn200题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
大衛說10 小时前
《Python 从入门到精通》系列总览与学习路线
开发语言·python·学习
可乐鸡翅yeah_11 小时前
hls.js 内存泄漏实战排查,直播 M3U8 长时间播放异常定位方案
开发语言·javascript·python·django·ecmascript·m3u8·m3u8在线
2601_9622035111 小时前
Java进阶07集合(续)
java·开发语言