NoFear

导航

字符串函数的实现

Posted on 2011-11-24 22:06  Fear_Hao  阅读(129)  评论(0编辑  收藏  举报
View Code
char* _strstr(char* haystack,char* needle)
{
//逐位
for(;;++haystack)
{
char* h = haystack;
for(char* n = needle;;++n,++h)
{
//匹配段空返回
if(!*n)
return haystack;
//1.先找到需要比较的开头 2.比较当前字符
if(*h !=*n)
break;
}
if(!*h)return NULL;
}
}
void *mymemcpy(void *dest, const void *src, size_t count)
{
if(dest==NULL || src==NULL)
{
return NULL;
}
char * temp_dest =(char*)dest;
char * temp_src = (char*)src;
while(count--)
{
*temp_dest++ = *temp_src++;
}
return dest;
}

char * mystrcpy(char * strDest,const char * strSrc)
{
if(strDest==NULL || strSrc==NULL)
{
return NULL;
}
char * temp_src =(char *)strSrc;
while( (*strDest++ = *temp_src++)!= '\0');
return strDest;

}

unsigned int mystrlen(const char *src)
{
if(src==NULL)
{
return NULL;
}
size_t count=;
while(*src++!='\0')
count++;
return count;

}