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 算法！！！