栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

strlen()函数原型: strlen( const char* string);

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

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

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

int main()
{
	char arr[] = "Hello World!!!";
	printf("%dn", strlen(arr));
	printf("%dn", my_strlen(arr));
	printf("%dn", my_self_strlen(arr));
}
strcpy()原型:

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

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

#include
char* my_strcpy(char* strDestination, const char* strSource)
{
	//1.参数检查 两个字符串都不能为空
	assert(strDestination != NULL && strSource != NULL);
	//2.保护参数
	char* temp = strDestination;
	//3.拷贝
	while (*strSource != '')
	{
		*temp = *strSource;
		temp++;
		strSource++;
	}
	*temp = '';//最后得给目标补一个 不然就会输出剩余的字符
	//4.返回值
	return strDestination;
}
//连同一起拷贝
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()函数的理解:就是一个连接函数,连接完之后别忘了加''
比如:将“abcd”连接到“defgh”中,最终“defgh”变为了“defghabcd”

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

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

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/655763.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号