模拟实现strlen()、strcat()、strcpy()和strcmp()

strlen()函数原型:

strlen( const char* string);

参数:一个字符串。
返回值:无符号的整数。
头文件:#include< string.h>。
作用:strlen用于求字符串的有效长度
对strlen函数的理解:1.用‘\0’的位置来判断字符串的有效长度。2.注意返回值是无符号的整数!
比如:“acde\0gh”,它的有效长度是4(即acde)

#include<stdio.h>
#include<string.h>
#include<assert.h>
//单变量模拟strlen
size_t my_self_strlen(const char* string)
{
	assert(string != NULL); 
	if (*string == '\0')
		return 0;
	return my_self_strlen(string + 1) + 1;
}

//自己模拟库函数(多个变量)
size_t my_strlen( const char* string)
{
	assert(*string != NULL);
	int count = 0;
	while (*string != '\0')
	{
		count++;
		string++;
	}
	return count;
}

int main()
{
	char arr[] = "Hello World!!!";
	printf("%d\n", strlen(arr));
	printf("%d\n", my_strlen(arr));
	printf("%d\n", my_self_strlen(arr));
}

strcpy()原型:

char* strcpy(char* strDestination, const char* strSource)

参数:两个字符串。
返回值:返回目标字符串的地址,即strDestination的地址
头文件:#include< string.h>。
作用:将源字符串拷贝到目标字符串。
对strcpy()函数的理解:就是一个拷贝函数,拷贝完之后别忘了加'\0'
比如:将“abcd”拷贝到“defgh”中,最终“defgh”变为了“abcd\0h”

#include<stdio.h>
char* my_strcpy(char* strDestination, const char* strSource)
{
	//1.参数检查 两个字符串都不能为空
	assert(strDestination != NULL && strSource != NULL);
	//2.保护参数
	char* temp = strDestination;
	//3.拷贝
	while (*strSource != '\0')
	{
		*temp = *strSource;
		temp++;
		strSource++;
	}
	*temp = '\0';//最后得给目标补一个\0 不然就会输出剩余的字符
	//4.返回值
	return strDestination;
}
//连同\0一起拷贝
int main()
{
	char arr[20] = "hello world!";
	char* s = "linux";
	//strcpy(arr,s);
	my_strcpy(arr, s);
	printf("%s",arr);
}

strcat()函数的原型:

char* strcat(char* strDestination, const char* strSource)

参数:两个字符串。
返回值:返回目标字符串的地址,即strDestination的地址
头文件:#include< string.h>。
作用:将源字符串连接到目标字符串的后面。
对strcat()函数的理解:就是一个连接函数,连接完之后别忘了加'\0'
比如:将“abcd”连接到“defgh”中,最终“defgh”变为了“defghabcd”

#include<stdio.h>
char* my_strcat(char* strDestination, const char* strSource)
{
	//1.断言
	assert(strDestination != NULL && strSource != NULL);
	//2.保护参数
	char* temp = strDestination;
	//3.计算
	while (*temp != '\0')
		temp++;//使目标字符串先到达\0位置 然后与源字符串连接
	while (*strSource != '\0')
	{
		*temp++ = *strSource++;
	}
	temp = '\0';  
	//4.返回值
	return strDestination;
}
int main()
{
	//char arr[] = "hello world!";
	//数组无法存储接下来的数据  需要给出数组长度
	char arr[20] = "hello";
	char* s = "linux";
	my_strcat(arr, s);
	printf("%s\n",arr);
}

strcmp()的原型:

int strcmp(const char* strDestnation, const char* strSource)

参数:两个字符串。
返回值: 
                目标字符串>源字符串,返回值为正数(即1)
                目标字符串=源字符串,返回值为0
                目标字符串<源字符串,返回值为负数(即-1)
头文件:#include< string.h>。
作用:将源字符串和目标字符串进行比较,比较每个字母ascii码的大小。
对strcat()函数的理解:就是一个连接函数,连接完之后别忘了加'\0'
比如:将目标字符串“abcd”与源字符串“defc”比较 因为a的ascii码<d的ascii码,因此返回值为负数(即-1)

#include<stdio.h>
int my_strcmp(const char* strDestnation, const char* strSource)
{
    assert(strDestnation != NULL && strSource != NULL);
	while (*strDestnation != '\0' && *strSource != '\0')  //两个都没走完
	{
		//相等的前提是两个必须走到最后一个位置才行
		if (*strDestnation > *strSource)
			return 1;
		else if (*strDestnation < *strSource)
			return -1;
		strDestnation++;
		strSource++;
	}
	//当其中一个走完
	if (*strDestnation != '\0')//如果这个不为0  那另外一个必定为0 不然跳不出while
		return 1;
	if (*strSource != '\0')
		return -1;
	return 0;
}
int main()
{
	char* arr1 = "HELLO ";
	char* arr2 = "HELLO";
	int result = my_strcmp(arr1,arr2);
	printf("%d\n", result);
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值