strcat strcpy strcmp 函数实现

自实现strcat strcpy strcmp函数记录
strcat :将两个字符串连接
strcpy:复制字符串
strcmp:比较字符串

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

char * m_strcat(char * dest, const char * source)
{
	//使用指针记录开始位置
	char * tmp = dest;
	while (*tmp != '\0')
	{
		//将指针移到末尾
		tmp++;
	}

	while (*source != '\0')
	{
		*tmp = *source;
		tmp++;
		source++;
	}
	return dest;
}

char * m_strcpy(char * dest, const char * source)
{
	if (dest == source)
	{
		return dest;
	}
	char * tmp = dest;

	while (*source != '\0')
	{
		*tmp = *source;
		tmp++;
		source++;
	}
	*tmp = '\0';
	return dest;
}

//应分情况,返回-1 0 1 
int m_strcmp(char * s1, char * s2)
{
	if (s1 == NULL || s2 == NULL)
	{
		return 0;
	}
	if (strlen(s1) != strlen(s2))
	{
		return 0;
	}
	while (*s1 != '\0')
	{
		if (*s1 != *s2)
		{
			return 0;
		}
		s1++;
		s2++;
	}
	return 1;
}


int main()
{
	char s1[10] = { "acbs" };
	char s2[20] = { "xxx" };
	cout << m_strcat(s1, s2) << endl;
	cout << s2 << endl;
	m_strcpy(s1, s2);
	cout << s1 << endl;
	m_strcmp(s1, s2);

	char s3[10] = { "aaaa" };
	char s4[10] = { "aaa" };
	cout << m_strcmp(s3, s4) << endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值