C语言字符串函数的使用与模拟实现(一)

strcpy函数的使用与模拟实现

strcpy简介

char * strcpy ( char * destination, const char * source );

strcpy函数用于将source指向的 C 字符串复制到destination指向的数组中,包括终止 null 字符(并在该点停止)。

#include<stdio.h>
#include<string.h>
int main()
{
	char a1[20] = { 0 };
	char a2[] = "hello world";
	strcpy(a1, a2);
	printf("%s", a1);
	return 0;
}

8f367b1d52744b4486dc5f022d3c5523.png

 注意:为防止溢出,应使目标字符串长度大于被复制字符串长度!

strcpy的模拟实现

#include<stdio.h>
void my_strcpy(char* destination, char* source)
{
	assert(destination != NULL);
	assert(source != NULL);
	while (*source != '\0')
	{
		*destination = *source;
		destination++;
		source++;
	}
}
int main()
{
	char a1[20] = { 0 };
	char a2[] = "hello world";
	my_strcpy(a1, a2);
	printf("%s", a1);
	return 0;
}

aa9f9951c186401ba97001c771455571.png

strcat函数的使用与模拟实现

strcat简介

char * strcat ( char * destination, const char * source );

source字符串的副本追加到destination字符串。destination 中的终止 '\0' 字符被 source 的第一个字符覆盖,并且在 destination 中由两者串联形成的新字符串的末尾包含一个 null 字符。返回destination字符串首地址。

#include<stdio.h>
#include<string.h>
int main()
{
	char a1[20] = " I love ";
	char a2[] = "China";
	strcat(a1, a2);
	printf("%s", a1);
	return 0;

}

35a19946ab4e4f7cabddca3e1c83f27d.png

注意:为防止溢出,destination字符串长度需大于两字符串长度之和+1(结尾须有'\0')!

strcat函数的模拟实现

#include<stdio.h>
#include<assert.h>
void my_strcat(char*destination,char*source)
{
   assert(destination != NULL);
   assert(source != NULL);
	 while (*destination != '\0')
		 destination++;
	 while (*source != '\0')
	 {
		 
		 *destination = *source;
		 destination++;
		 source++;
	 }
	
}
int main()
{
	char a1[20] = "I love ";
	char a2[] = "China";
	my_strcat(a1, a2);
	printf("%s", a1);
	return 0;
}

87961de56b344c718c7c14d9f75a7eef.png

strlen函数的使用与模拟实现

strlen函数简介

size_t strlen ( const char * str );

strlen函数用于计算一个字符串的长度,由该字符串第一个字符数到第一个'\0'字符,单位为字符个数。

#include<stdio.h>
#include<string.h>
#include<assert.h>
int main()
{
	char a1[20] = "I love China";
	int len=strlen(a1);
	printf("%d", len);
	return 0;

}

6c6aaabf45e0475fb74e551c175cc551.png

注意区分计算字符串长度与字符串大小!!!

strlen函数用于计算字符串函数,而sizeof操作符则是计算字符串数组大小,例如,a[20]="I love China",长度为12个字符,而大小则为开辟的20个字节。注意区分二者区别。

5d411db067e745e6b728b5aaafed474b.png

strlen函数的模拟实现

#include<stdio.h>
size_t my_strlen(char*str)
{
	int count = 0;
	while (*str != '\0')
	{
		count++;
		str++;
	}
	return count;
}
int main()
{
	char a1[20] = "I love China";
	int len= my_strlen(a1);
	printf("%d\n", len);
	return 0;
}

2f2857f8779c422288cde6ff67cb69f5.png strcmp函数的使用与模拟实现

strcmp函数简介

int strcmp ( const char * str1, const char * str2 );

将字符串 str1 与\字符串 str2 进行比较。
此函数开始比较每个字符串的第一个字符。如果它们彼此相等,则继续对比第二个字符,以此类推,直到字符不同或达到终止字符。字符间大小比较字符的ascll值,若str1>str2,返回大于0的数;若小于,返回小于0的数;若相等,返回0;

#include<stdio.h>
#include<string.h>
int main()
{
	char a1 [20] = "I love China";
	char a2 [20] = "I love Chinese";
	printf("%d", strcmp(a1, a2));
	return 0;
}

56a8e96e304f4f48a83fde59e25f8d24.png

因为字符a1[12]---a小于字符a2[12]---e,所以判断a1<a2,返回负数。

444b6089c0a04311b6faf93279d1628b.png

因为第一个字符a1[1]---a小于a2[1]---b,所以判断a1<a2,返回一个负数。

201ba85a04e54ba8b07c6e066fdb2c0f.png

因为a1与a2每个字符都相同,判断二者相等,返回0。

注意:strcmy逐一比较各个字符,与字符串长短并无直接关系。

strcmp函数的模拟实现

#include<stdio.h>
#include<string.h>
int my_strcmp(char* str1, char* str2)
{
	while (*str1 && *str2)
	{
		if (*str1 > *str2)
			return 1;
		else if (*str1 < *str2)
			return -1;
		else
		{
			str1++;
			str2++;
		}
	}
	return (*str1 - *str2);

}
int main()
{
	char a1 [20] = "China";
	char a2 [20] = "China NO.1";
	printf("%d", my_strcmp(a1, a2));
	return 0;
}

832939c2b189435fa177b911ad80a9ab.png

84522d51ca46457399370671c768cc65.png

未完待续,如有错漏,敬请谅解。

  • 59
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值