四种函数的实现:
strcmp:
int my_strcmp(char *s1, char *s2)
{
while(*s1 != '\0' && *s2 != '\0')
{
if(*s1 > *s2)
{
return 0;
}
else if(*s1 < *s2)
{
return -1;
}
}
s1++;
s2++;
if(*s1 == '\0' && *s2 == '\0')
{
return 0;
}
else if(*s1 == '\0' && *s2 != '\0')
{
return -1;
}
else if(*s1 != '\0' && *s2 == '\0')
{
return 1;
}
}
strcat:
char *my_strcat(char *dest,char *src)
{
char *temp = dest;
while(*temp != '\0')
{
temp++;
}
while(*src != '\0')
{
*temp = *src;
temp++;
src++;
}
*temp = '\0';
return dest;
}
strcpy:
char * my_strcpy(char *dest, char *src)
{
int i;
for(i = 0; *(src + i) != '\0'; i++)
{
*(dest + i) = *(src + i);
}
*(dest + i) = '\0';
return dest;
}
strncpy:
char *my_strncpy(char *dest, char *src, int len)
{
int i;
if(my_strlen(src) < len)
{
return dest;
}
for(i = 0; i < len; i++)
{
*(dest + i) = *(src + i);
}
return dest;
}