【零散随笔】C语言字符串操作函数的模拟实现

/*
* 重新封装C字符串的部分函数
* 包括strlen,strcpy,strcat,strcmp,strchr(strcpy,strcat,strchr查阅资料之后进行了优化)
* memcpy,memmove
* strncpy,strncat,strncmp
* KMP算法实现strstr暂未完成   2019.4.18
*/

#include
#include
#include

size_t mystrlen(const char* str)
{
	size_t count = 0;
	if (str = nullptr)
		return count;
	while (*str != '\0')
		count++; str++;
	return count;
}

char* mystrcpy(char* dest, const char* source)
{
	if (source == nullptr || dest == nullptr)
		return NULL;
	while (*source != '\0'){
		*dest = *source;
		dest++; source++;
	}
	*dest = '\0';
	return dest;
}

char* mystrcpyV2(char* dest, const char* source)
{
	if (source == nullptr || dest == nullptr)
		assert(NULL);
	//保护目标串值??为什么?dest不就是临时变量么?
	char* tmp = dest;
	//while ((*tmp++ = *source++) != '\0');
	while (*tmp++ = *source++);
	return tmp;
}

char* mystrcat(char* dest, const char* source)
{
	if (source == nullptr || dest == nullptr)
		return NULL;
	while (*dest != '\0')
		dest++;
	mystrcpy(dest, source);
	return dest;
}

char* mystrcatV2(char* dest, const char* source)//faststrcat
{
	if (source == nullptr || dest == nullptr)
		assert(NULL);
	//while (*dest++)这是个坑,不能这样写,会使dest多++一次,dest原来的/0没有被覆盖
	while (*dest)
		dest++;
	while (*dest++ = *source++);
	//FastStrcat函数每次返回拼接后的字符串尾部,即指向结束符所在位置。
	//下次调用时便不再需要扫描整串,从而提高执行效率。可以优化第一层循环找\0的过程。
	return --dest;
}

char* mystrchr(char* str, int ascll)
{
	if (str == nullptr){
		return NULL;
	}
	while (*str != '\0'){
		if ((int)*str == ascll){
			char *cutstr = str;
			return cutstr;
		}str++;
	}return NULL;
}

char* mystrchrV2(char *s, char c)
{
	assert(s);
	while (*s != '\0' && *s != c)
		++s;
	return *s == c ? s : NULL;
}

int mystrcmp(const char* str1, const char* str2)
{
	if (str1 == nullptr || str2 == nullptr)
		return NULL;
	while (*str1 == *str2)
	{
		if (*str1 == '\0')
			return 0;
		str1++; str2++;
	}
	return *str1 > *str2 ? 1 : -1;
}

void * mymemcpy(void * dest, const void * source, size_t num)
{
	void* ret = dest;
	while (num--){
		*(char*)dest = *(char*)source;
		dest = (char*)dest + 1;//(char*)dest++;为什么这样写会报错???
		source = (char*)source + 1;
	}
	return ret;
}

void* mymemmove(void * dst, const void * src, size_t count)
{
	void * ret = dst;
	if (dst <= src || (char *)dst >= ((char *)src + count)) {
		//不重叠
		while (count--) {
			*(char *)dst = *(char *)src;
			dst = (char *)dst + 1;
			src = (char *)src + 1;
		}
	}
	else {//重叠
		dst = (char *)dst + count - 1;//此处-1是个细节,不然会多拷一个单位
		src = (char *)src + count - 1;
		while (count--) {
			*(char *)dst = *(char *)src;
			dst = (char *)dst - 1;
			src = (char *)src - 1;
		}
	}
	return(ret);
}

//strstr by kmp
//char * mystrstr(const char *, const char *);

char * mystrncpy(char * destination, const char * source, size_t num)
{
	if (source == nullptr || destination == nullptr)
		assert(NULL);
	char* Dtmp = destination;
	while (*source != '\0'&& num > 0){
		num--;
		*Dtmp++ = *source++;
	}*Dtmp = '\0';
	return destination;
		
}

char * mystrncat(char * destination, const char * source, size_t num)
{
	if (source == nullptr || destination == nullptr)
		assert(NULL);

	char* tmp = destination;
	while (*tmp != '\0'){
		tmp++;
	}
	while (num > 0){
		num--;
		*tmp++ = *source++;
	}*tmp = '\0';
	return destination;
}

int mystrncmp(const char* str1, const char* str2, size_t num)
{
	if (str1 == nullptr || str2 == nullptr)
		return NULL;
	while (num > 0){
		num--;
		if (*str1 != *str2 || *str1 == '\0' || *str2 == '\0')
			return *str1 > *str2 ? 1 : -1;
	}
	return 0;
}

 

你可能感兴趣的:(c++)