#include <stdio.h>
#include <assert.h>
#include <string.h>
char *my_strcpy(char *StrDest,const char *StrSrc)
{
assert((NULL != StrDest)&&(NULL != StrSrc));
char *str = StrDest;
while((*StrDest++ = *StrSrc++)!='\0');
return str;
}
size_t mystrlen(const char *str)
{
assert(NULL != str);
int count=0;
while((*str++) != '\0')
{
count++;
}
return count;
}
char *my_strcat(char *dest, const char *src)
{
assert((NULL!=dest)&&(NULL!=src));
char *str = dest;
while(*str)
str++;
while((*str++ = *src++));
return dest;
}
int my_strcmp(const char *s1,const char *s2)
{
while(*s1==*s2)
{
assert((s1!=NULL)&&(s2!=NULL));
if(*s1=='\0')
return 0;
s1++;
s2++;
}
return *s1-*s2;
}
char *my_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;
}
void *my_memset(void *s,int c,unsigned int n)
{
const unsigned char uc = c;
unsigned char *su = s;
for (su = s ; 0 < n; --n,su++) {
*su = uc;
}
return s;
}
void *my_memcpy(void *dest,const void *src,unsigned int n)
{
assert((NULL!=dest)&&(NULL!=src));
void *str = dest;
if(dest <= src || (char *)dest >= (char * )src + n)
{
printf("没有内存重叠,从低地址开始复制\n");
while(n--){
*(char *)str = *(char *)str;
str = (char *)str + 1;
src = (char *)src + 1;
}
}else{
printf("有内存重叠,从高地址开始复制\n");
str = (char *)str + n - 1;
src = (char *)src + n - 1;
while(n--){
str = (char *)str - 1;
src = (char *)src - 1;
}
}
return str;
}
long my_atol(const char *nptr)
{
int num = 0;
while(*nptr)
{
num = 10 * num + *nptr-'0';
nptr++;
}
return num;
}
char *reversal_string(char *str)
{
assert(NULL != str);
char *cp = str;
char temp;
int n = strlen(str)-1;
char *pstr = & cp[n];
while(cp < pstr)
{
temp = *cp;
*cp = *pstr;
*pstr = temp;
cp++;
pstr--;
}
return cp;
}
char *reversal_str_word(char *str,char *pstr)
{
assert(NULL != str);
char temp;
while(str<pstr)
{
temp = *str;
*str = *pstr;
*pstr = temp;
str++;
pstr--;
}
return str;
}
char *reversal_word(char *str)
{
assert(NULL != str);
char *cp = str;
int n = strlen(str)-1;
char *pstr = &cp[n];
reversal_str_word(cp,pstr);
while(*cp)
{
char *start = cp;
while(*cp != ' ' && *cp != '\0')
cp++;
reversal_str_word(start,cp-1);
cp ++;
}
return str;
}
|