Tim的博客

Tim的学习手册

homework_19

char* my_srcpy(char* dest, const char* src){
	char* ret = dest;
	assert(dest != NULL&& src != NULL);
	while (*dest++ = *src++);
	return ret;
}
char* my_strcat(char* dest, const char* src){
	char* ret = dest;
	assert(dest != NULL&& src != NULL);
	while (*dest++);
	while (*dest++ = *src++);
	return ret;
}
char* my_strstr(const char *src, const char *substr){
	
	const char *s1 = NULL;
	const char *s2 = NULL;
	const char *cur = src;
	assert(src != NULL && substr != NULL);

	if (*substr == '\0'){
		return (char*)src;
	}
	while (*cur){
		s1 = cur;
		s2 = substr;
		while (*s1 && *s2 && *s1==*s2){ //父字符串结束了退出循环、子字符串结束了退出循环、不相等的时候退出循环
			s1++;
			s2++;
		}
		//if (*s2){ 此处问题待考虑....
		if (*s2 == '\0'){
			//只有s2结束时才算找到子字符串了
			return (char*)cur;
		}
		cur++;
	}
	return NULL;
}
int my_strcmp(const char* cmp, const char* src){
	assert(cmp != NULL && src != NULL);
	while (*cmp++ && *src++){
		if (*cmp != *src){
			return  *cmp - *src;
		}
	}
	return 0;
}
void* my_memcpy(void* dest, const void* src, size_t count){
	char* dest2 = (char*)dest;
	char* src2 = (char*)src;

	assert((dest != NULL) && (src != NULL) && (count > 0));

	while (count--){
		*dest2++ = *src2++;
	}
	return dest;
}
void* my_memmove(void *dest,const void	*src,size_t count){
	char *dest2 = (char*)dest;
	char *src2 = (char*)src;

	assert(dest != NULL && src != NULL && count > 0);

	if (dest < src){
		//从前往后copy
		while (count--){
			*dest2++ = *src2++;
		}
	}
	else{
		//从后往前copy
		while (count--){
			*(dest2 + count) = *(src2 + count);
		}
	}
	return dest;
}
const char* my_strchr(char ch, const char* src){
	assert(ch != 0 && src != NULL);
	while (*src++){
		if (ch == *src){
			return src;
		}
	}
	return NULL;
}

KMP 算法!!!




阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38032942/article/details/80335214
文章标签: 作业
上一篇homework_17
下一篇homework_18
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭