1.strlen(字符串的长度)
size_t Strlen(const char* str)
{
assert(str);
for (size_t i = ;; ++i)
{
if (str[i] == '\0')
return i;
}
} size_t StrlenByRecursion(const char* str)
{
return *str == '\0' ? : StrlenByRecursion(str + ) + ;
}
size_t __cdecl strlen (const char * str) //函数库里面实现的代码
{
const char *eos = str; while( *eos++ ) ; return( (int)(eos - str - 1) );
}
2.strcpy(字符串拷贝)
char* Strcpy1(char* dst, const char* src)
{
assert(dst && src);
int i = ;
for (; src[i] != '\0'; ++i)
{
dst[i] = src[i];
}
dst[i] = '\0';
return dst;
} char* Strcpy2(char* dst, const char* src)
{
assert(dst && src);
char* tmp = dst;
while ((*tmp++ = *src++) != '\0');
*tmp = '\0';
return dst;
} char * strcpy (char * dest,const char* src) //函数库中的实现
{
reg_char c;
char *__unbounded s = (char *__unbounded) CHECK_BOUNDS_LOW (src);
const ptrdiff_t off = CHECK_BOUNDS_LOW (dest) - s - 1;
size_t n;
do
{
c = *s++;
s[off] = c;
}
while (c != '\0');
n = s - src;
(void) CHECK_BOUNDS_HIGH (src + n);
(void) CHECK_BOUNDS_HIGH (dest + n);
return dest;
}
3.strncpy
char* Strncpy(char* dst,const char* src,int lenth)
{
assert(dst && src);
char* tmp = dst;
while (lenth--)
{
if ((*tmp++ = *src) != '\0')
src++;
}
*tmp = '\0';
return dst;
}
char * __cdecl strncpy (char * dest,const char * source,size_t count)
{
char *start = dest; while (count && (*dest++ = *source++)) /* copy string */
count--; if (count) /* pad out with zeroes */
while (--count)
*dest++ = '\0'; return(start);
}
4.strcat(字符串连接)
char* Strcat(char* dst, const char* src)
{
assert(dst && src);
char* tmp = dst;
while (*tmp)
tmp++;
while ((*tmp++ = *src++) != '\0');
return dst;
}
char * __cdecl 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 */
}
5.strncat
char* Strncat(char* dst,const char* src,size_t lenth)
{
assert(dst && src);
char* tmp = dst;
while (*tmp)
++tmp;
while (lenth-- && (*tmp++ = *src++));
*tmp = '\0';
return dst;
}
char * __cdecl strncat (char * front,const char * back,size_t count) //库中实现的方法
{
char *start = front; while (*front++)
;
front--; while (count--)
if (!(*front++ = *back++))
return(start); *front = '\0';
return(start);
}
6.strcmp
int Strcmp(const char* str1, const char* str2)
{
assert(str1 && str2);
while (*str1 == *str2 && *str2)
{
str1++;
str2++;
}
return *(unsigned char*)str1 - *(unsigend char*)str2;
}
int __cdecl 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 );
}
7.strncmp
int Strncmp(const char* str1, const char* str2,size_t lenth)
{
assert(str1 && str2);
while (lenth-- && *str2)
{
if (*str1 != *str2)
return *(unsigned char*)str1 - *(unsigned char*)str2;
str1++;
str2++;
}
return ;
}
int __cdecl strncmp (const char * first,const char * last,size_t count) //库里实现的方式
{
if (!count)
return(0); while (--count && *first && *first == *last)
{
first++;
last++;
} return( *(unsigned char *)first - *(unsigned char *)last );
}