当前位置:网站首页>[Cultivation of internal skills of string functions] strcpy + strcat + strcmp (1)
[Cultivation of internal skills of string functions] strcpy + strcat + strcmp (1)
2022-08-04 22:50:00 【Albert Edison】


前言
C 语言中对字符和字符串的处理很是频繁,但是 C 语言本身是没有字符串类型的,字符串通常放在 常量字符串 中或者 字符数组 中.
字符串常量 适用于那些对它不做修改的字符串函数;
This article will focus on processing 字符 和 字符串 The use and precautions of the library functions
1. strcpy - 字符串拷贝
函数介绍
char* strcpy(char* Destination, const char* Source);
1、strcpy 是将 Source 字符串 的内容拷贝到 Destination 字符串 中;
2、源字符串必须以 \0 结束;
3、会将源字符串中的 \0 一并拷贝到 目标 空间;
4、目标空间必须足够大,以确保能存放源字符串;
5、目标空间必须可变;
代码示例
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "hawkeye";
char str2[20] = "EEEEEEEEEEEEEEEE";
strcpy(str2, str1);
printf("%s\n", str2);
return 0;
}
运行结果
可以看到
\0也是拷贝到 str2 中了
模拟实现
代码示例
#include <stdio.h>
#include <assert.h>
char* my_strcpy(char* dest, const char* src) {
char* start = dest;
assert(dest && src);
while (*src) {
*dest = *src;
dest++;
src++;
}
*dest = *src; //此时src中还剩下\0,赋值给dest
return start;
}
int main()
{
char str1[] = "hawkeye";
char str2[20] = "EEEEEEEEEEEEEEEE";
my_strcpy(str2, str1);
printf("%s\n", str2);
return 0;
}
运行结果
代码升级
#include <stdio.h>
#include <assert.h>
char* my_strcpy(char* dest, const char* src) {
char* start = dest;
assert(dest && src);
while (*dest++ = *src++) {
;
}
return start;
}
int main()
{
char str1[] = "hawkeye";
char str2[20] = "EEEEEEEEEEEEEEEE";
my_strcpy(str2, str1);
printf("%s\n", str2);
return 0;
}
因为是后置 ++,所以 *src content first *dest 中,然后 dest++、src++,The address of the second element is found…以此类推;
当 src 指向 \0 时,src 赋值给 dest,此时 while The result of the entire expression inside the loop is \0,而 \0 的 ASCII 码值是 0,So the cycle stops;
为什么要定义 strat 呢?
因为 strcpy 返回的是 目标字符串 的起始地址,而当 while 循环结束以后,dest It is no longer the starting address;
So save a copy in advance dest starting address to strat 中.
运行结果
2. strcat - 字符串追加
函数介绍
char* strcat(char* destination, const char* source);
1、strcat 是用来 Connection string 的,It will take parameters source 字符串复制到参数 dest 所指的字符串尾部.
2、源字符串必须以 \0 结束.
3、会将源字符串中的 \0 拷贝到目标空间.
4、目标空间必须足够大,以确保能存放源字符串.
5、目标空间必须可变.
代码示例
#include <stdio.h>
#include <string.h>
int main()
{
char str1[30] = "hello";
char str2[] = "world";
strcat(str1, str2);
printf("%s\n", str1);
return 0;
}
运行结果
模拟实现
代码示例
#include <stdio.h>
#include <assert.h>
char* my_strcat(char* dest, const char* src) {
char* start = dest;
assert(dest && src);
//1. 目标空间中的\0
while (*dest) {
dest++;
}
//2. 追加内容到目标空间
while (*dest++ = *src++) {
;
}
return start;
}
int main()
{
char str1[30] = "hello";
char str2[] = "world";
my_strcat(str1, str2);
printf("%s\n", str1);
return 0;
}
运行结果
3. strcmp - 字符串比较
函数介绍
int strcmp(const char* str1, const char* str2);
strcmp 用于对 Two sets of strings are compared 的函数,比较的是对应位置上的字符大小,它的返回值是 int 类型.
Compare regulations:
- 第一个字符串大于第二个字符串,则返回大于 0 的数字;
- 第一个字符串等于第二个字符串,则返回 0;
- 第一个字符串小于第二个字符串,则返回小于 0 的数字
代码示例
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "abcdef";
char str2[] = "bbq";
int ret = strcmp(str1, str2);
printf("%d\n", ret);
return 0;
}
运行结果
模拟实现
代码示例
#include <stdio.h>
#include <assert.h>
int my_strcmp(const char* str1, const char* str2) {
assert(str1 && str2);
while (*str1 == *str2) {
//如果两个字符串相等,put some string\0,Take it back
if (*str1 == '\0') {
return 0;
}
str1++;
str2++;
}
if (*str1 > *str2) {
return 1;
}
else {
return -1;
}
}
int main()
{
char str1[] = "abz";
char str2[] = "abq";
int ret = my_strcmp(str1, str2);
printf("%d\n", ret);
return 0;
}
运行结果
代码升级
#include <stdio.h>
#include <assert.h>
int my_strcmp(const char* str1, const char* str2) {
assert(str1 && str2);
while (*str1 == *str2) {
//如果两个字符串相等,It means they have all gone\0的位置
//然后\0 = \0,进行循环,直接进入if语句
if (*str1 == '\0') {
return 0;
}
str1++;
str2++;
}
return *str1 - *str2;
}
int main()
{
char str1[] = "abcdef";
char str2[] = "bbq";
int ret = my_strcmp(str1, str2);
if (ret < 0) {
printf("str1 < str2");
}
else if (ret > 0) {
printf("str1 > str2");
}
else {
printf("str1 == str2");
}
return 0;
}
运行结果
边栏推荐
猜你喜欢
随机推荐
typeScript-部分应用函数
To Offer | 03. Repeat Numbers in the array
逆序对的数量
[Mock Interview - 10 Years of Work] Are more projects an advantage?
【3D建模制作技巧分享】ZBrush纹理贴图怎么导入
enumerate()函数
Controller层代码这么写,简洁又优雅!
SRv6网络的安全解决方案
promise详解
good luck
Deep Learning RNN Architecture Analysis
360市值四年蒸发3900亿,政企安全能救命吗?
PowerBI scripture series
Qt中的常用控件
文章占位 文章占位
期货开户哪个平台好,要正规安全的
后排乘客不系安全带?事故瞬间被甩出
线上虚拟展馆展示具有哪些优势
论文解读(PPNP)《Predict then Propagate: Graph Neural Networks meet Personalized PageRank》
MySQL的JSON 数据类型2


















