实现strlen
#############################################
int my_strlen(const char* str)
{
int count = 0;
while(*str)
{
count++;
str++;
}
return count;
}
***********************************************
int my_strlen(const char* str)
{
if(*str)
{
return 0;
}
else
return 1+my_strlen(str+1);
}
************************************************
int my_strlen(const char* str)
{
const char *p = s;
while(*p != '\0')
p++;
return p-s;
}
size_t __cdecl strlen (
const char * str
)
{
const char *eos = str;
while( *eos++ ) ;
return( eos - str - 1 );
}
####################################################
size_t __cdecl strnlen(const char *str, size_t maxsize)
{
size_t n;
for (n = 0; n < maxsize && *str; n++, str++)
;
return n;
}
#########################################################
实现strcpy
char* my_strcpy(char* dst,const char* src)
{
char* ret = dst;
assert(dst != NULL);
assert(src != NULL);
while((*dst++ = *src++))
;
return ret;
}
********************************************************
char * __cdecl strncpy (char * dest,const char * source,size_t count)
{
char *start = dest;
while (count && (*dest++ = *source++))
count--;
if (count)
while (--count)
*dest++ = '\0';
return(start);
}
#########################################################
实现strcat
char* my_strcat(char* dst,const char* src)
{
char* ret = dst;
assert(dst!= NULL);
assert(src != NULL);
while(*dst )
{
dest++;
}
while(*dst++ = *src++)
;
return ret;
}
*******************************************************
把src所指字符串的前n个字符添加到dest所指字符串的结尾处,并覆盖dest所指字符串结尾的'\0',从而实现字符串的连接。
src和dest所指内存区域不可以重叠,并且dest必须有足够的空间来容纳src的字符串。
char* 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);
}
#########################################################
实现strstr
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 = (char *) str2;
while ( *s1 && *s2 && !(*s1-*s2) )
s1++, s2++;
if (!*s2)
return(cp);
cp++;
}
return(NULL);
}
#########################################################
实现strcmp
int my_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;
}
***********************************************************
int my_strncmp(const char* first,const char* last,size_t n)
{
int result = 0;
if(first == NULL || last == NULL || n < 0)
return fasle;
if(n == 0)
return 0;
while(n-- &&!(result = *(unsigned char*)first - *(unsigned char*)last) && *last)
{
last++;
first++;
}
return ret;
}
##########################################################
void* memcpy(void *dst,const void *src,size_t count)
{
void *ret = dst;
while(count--)
{
*(char*)dst = *(char*)src;
dst = (char*)dst + 1;
src = (char*)src + 1;
}
return ret;
}
##########################################################
void * memmove(void* dst,const void* src,size_t count)
{
void * ret = dst;
if(dst <= src || (char*)dst >= ((char*)src + count))
{
while(count--)
{
*(char*) dst = *(char*)src;
dst = (char*)dst + 1;
src = (char*)src + 1;
}
}
else{
dst = (char*)dst + count -1;
src = (char*)src + count -1;
while(count--)
{
*(char*)dst = *(char*)src;
dst = (char*)dst - 1;
src = (char*)src - 1;
}
}
return ret;
}