最近查看了微软的c标准库针对字符串操作的部分源代码,整理了一下放上来
char * strcat(char * dst, const char * src)
{
char * cp = dst;
while(*cp)
{
cp++; /* find end of dst */
}
while (*cp++ = *src++); /* Copy src to end of dst */
return dst; /* return dst */
}
char * strcpy(char * dst, const char * src)
{
char * cp = dst;
while (*cp++ = *src++); /* Copy src over dst */
return dst;
}
char * strchr(const char * string, int ch)
{
while (*string && *string != (char)ch)
{
string++;
}
if (*string == (char)ch;
return (char *)string;
return NULL;
}
int strcmp(const char * src, const char * dst)
{
int ret = 0;
while (!(ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
++src, ++dst;
if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;
return( ret );
}
size_t strlen(const char * str)
{
const char * eos = str;
while (*eos++);
return (int)(eos - str - 1);
}
char * strstr(const char * str1, const char * str2)
{
char * cp = (char *)str1;
char *s1, *s2;
if (!*str2)
return (char *)str1;
while (*cp)
{
s1 = cp;
s2 = str2;
while (*s1 && *s2 && !(*s1-*s2))
s1++, s2++;
while (!*s2)
return cp;
cp++;
}
return NULL;
}
posted on 2008-03-27 09:47
水 阅读(1507)
评论(5) 编辑 收藏 引用 所属分类:
c/c++基础知识