#include<stdio.h>
#include<assert.h>
#include<string.h>
typedef unsigned int size_t;
int strnCmp(const char * s1, const char *s2 , size_t count)
{
if(!s1 || !s2)
printf("string is null\n");
while( !(*s1-*s2) && *s1!='\0' && *s2!='\0'&& --count)
{
s1++;
s2++;
// count--;
}
if(*s1-*s2==0)
return 0;
if(*s1-*s2>0)
return 1;
else if(*s1-*s2<0)
return -1;
}
int strCmp(const char *s1,const char *s2)
{
int result;
while(1)
{
if( (result=*s1-*s2) !=0 )
return result;
if( *s1 =='\0' && *s2=='\0')
return 0;
s1++;
s2++;
}
}
size_t myStrlen(const char *s)
{
assert( s !=NULL);
int len=0;
while(*s!='\0')
{
len++;
s++;
}
return len;
}
char * myStrcpy(char *dest,const char *src)
{
assert(dest!=NULL && src!=NULL);
while( (*dest++ =*src++ )!='\0')
{
;
}
// *(dest++)='\0'; 比这种方式更好,少了分情况讨论
return dest;
}
char * myStrncpy(char *dest,const char *src, size_t n)
{
assert(dest!=NULL && src!=NULL);
while(n )
{
*dest++=*src++;
n--;
}
// *(dest++)='\0';
return dest;
}
char * myStrcat(char * dest ,const char * src)
{
assert(dest!=NULL && src!=NULL);
while( *dest !='\0')
dest ++;
while( (*dest++ = *src++)!='\0' )
;
return dest;
}
void *myMemcpy(void * dest,const void *src,size_t count)
{
assert(dest!=NULL && src!=NULL);
while(count )
{
*(char *) dest++ =*(char *)src++;
count--;
}
return dest;
}
int main()
{
char str[10];
char dest[10]="ABCDE";
char str1[10]="ABCDE";
scanf("%s",str);
/*
int a=strlen(str);
printf("the lib strlen is %d\n",a);
int b=myStrlen(str);
printf("the my strlen is %d\n\n\n",b);
printf("the original :%s\n",str);
myStrcpy(dest,str);
printf("after the my copy :%s\n",dest);
strcpy(str1,str);
printf("after the lib copy :%s\n\n\n",str1);
printf("the original :%s\n",str);
myStrncpy(dest,str,3);
printf("after the my ncopy :%s\n",dest);
strncpy(str1,str,3);
printf("after the lib ncopy :%s\n",str1);
printf("the original :%s\n",str);
myStrcat(dest,str);
printf("after the my strcat :%s\n",dest);
strcat(str1,str);
printf("after the lib strcat :%s\n",str1);
printf("the original :%s\n",str); //注意测试数据记得选择比src 大,小,相等
myMemcpy(dest,str,3);
printf("after the my memcpy :%s\n",dest);
memcpy(str1,str,3);
printf("after the lib memcpy :%s\n",str1);
*/
int a[3]={0}; //int 作为测试数据
int b[3]={0};
int i;
myMemcpy(a,str,5);
printf("after the my memcpy :\n");
for(i=0 ;i<sizeof(a);i++) //其实应该把sizeof(a)提到外面,否则性能低下,重复调用频繁
printf("%d ",* (( char *)a+i) );
printf("\n\n");
memcpy(b,str,5);
printf("after the lib memcpy :\n");
for(i=0 ;i<sizeof(b);i++)
printf("%d ", * ( ( char *)b+i) );
return 1;
}
#include<assert.h>
#include<string.h>
typedef unsigned int size_t;
int strnCmp(const char * s1, const char *s2 , size_t count)
{
if(!s1 || !s2)
printf("string is null\n");
while( !(*s1-*s2) && *s1!='\0' && *s2!='\0'&& --count)
{
s1++;
s2++;
// count--;
}
if(*s1-*s2==0)
return 0;
if(*s1-*s2>0)
return 1;
else if(*s1-*s2<0)
return -1;
}
int strCmp(const char *s1,const char *s2)
{
int result;
while(1)
{
if( (result=*s1-*s2) !=0 )
return result;
if( *s1 =='\0' && *s2=='\0')
return 0;
s1++;
s2++;
}
}
size_t myStrlen(const char *s)
{
assert( s !=NULL);
int len=0;
while(*s!='\0')
{
len++;
s++;
}
return len;
}
char * myStrcpy(char *dest,const char *src)
{
assert(dest!=NULL && src!=NULL);
while( (*dest++ =*src++ )!='\0')
{
;
}
// *(dest++)='\0'; 比这种方式更好,少了分情况讨论
return dest;
}
char * myStrncpy(char *dest,const char *src, size_t n)
{
assert(dest!=NULL && src!=NULL);
while(n )
{
*dest++=*src++;
n--;
}
// *(dest++)='\0';
return dest;
}
char * myStrcat(char * dest ,const char * src)
{
assert(dest!=NULL && src!=NULL);
while( *dest !='\0')
dest ++;
while( (*dest++ = *src++)!='\0' )
;
return dest;
}
void *myMemcpy(void * dest,const void *src,size_t count)
{
assert(dest!=NULL && src!=NULL);
while(count )
{
*(char *) dest++ =*(char *)src++;
count--;
}
return dest;
}
int main()
{
char str[10];
char dest[10]="ABCDE";
char str1[10]="ABCDE";
scanf("%s",str);
/*
int a=strlen(str);
printf("the lib strlen is %d\n",a);
int b=myStrlen(str);
printf("the my strlen is %d\n\n\n",b);
printf("the original :%s\n",str);
myStrcpy(dest,str);
printf("after the my copy :%s\n",dest);
strcpy(str1,str);
printf("after the lib copy :%s\n\n\n",str1);
printf("the original :%s\n",str);
myStrncpy(dest,str,3);
printf("after the my ncopy :%s\n",dest);
strncpy(str1,str,3);
printf("after the lib ncopy :%s\n",str1);
printf("the original :%s\n",str);
myStrcat(dest,str);
printf("after the my strcat :%s\n",dest);
strcat(str1,str);
printf("after the lib strcat :%s\n",str1);
printf("the original :%s\n",str); //注意测试数据记得选择比src 大,小,相等
myMemcpy(dest,str,3);
printf("after the my memcpy :%s\n",dest);
memcpy(str1,str,3);
printf("after the lib memcpy :%s\n",str1);
*/
int a[3]={0}; //int 作为测试数据
int b[3]={0};
int i;
myMemcpy(a,str,5);
printf("after the my memcpy :\n");
for(i=0 ;i<sizeof(a);i++) //其实应该把sizeof(a)提到外面,否则性能低下,重复调用频繁
printf("%d ",* (( char *)a+i) );
printf("\n\n");
memcpy(b,str,5);
printf("after the lib memcpy :\n");
for(i=0 ;i<sizeof(b);i++)
printf("%d ", * ( ( char *)b+i) );
return 1;
}