C 语言实例 - 字符串复制

C 语言教程 · 2019-02-21 09:59:03

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

实例 - 使用 strcpy()

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

输出结果为:

最终的目标字符串: This is codercto.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: codercto
字符串 s2: codercto

点击查看所有 C 语言教程 文章: https://www.codercto.com/courses/l/17.html

查看所有标签

Letting Go of the Words

Letting Go of the Words

Janice (Ginny) Redish / Morgan Kaufmann / 2007-06-11 / USD 49.95

"Redish has done her homework and created a thorough overview of the issues in writing for the Web. Ironically, I must recommend that you read her every word so that you can find out why your customer......一起来看看 《Letting Go of the Words》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具