字符串操作基础函数实现

#include <assert.h>
#include <stdio.h>
using namespace std;


char* strcpy(char* des, const char* src)
{
	assert((des != NULL) && (src != NULL));
	char* address = des;
	while ((*des++ = *src++) != '\0');
	return address;
}

int strlen(const char* str)
{
	assert(str != NULL);
	int len = 0;
	while ((*str++) != '\0')
		++len;
	return len;
}

char* strcat(char* des, const char* src)   // const表明为输入参数 
{
	assert((des != NULL) && (src != NULL));
	char* address = des;
	while (*des != '\0')  ++des;
	while (*des++ = *src++)
		;
	return address;
}

int strcmp(const char* s1, const char* s2)
{
	assert((s1 != NULL) && (s2 != NULL));
	while (*s1 == *s2)
	{
		if (*s1 == '\0')
			return 0;

		++s1;
		++s2;
	}
	return *s1 - *s2;
}


int main()
{
	char des1[7] = "123";
	const char* des = "123";
	const char* src = "456";
	strcat(des1, des);
	printf("cpy = %s \n", strcpy(des1, src));
	return 0;
}

  

上一篇:我对DES AES RSA的认识


下一篇:ASP.NET加密算法(MD5/DES)