strlen
int mystrlen(const char *src)
{
int len = 0;
while(*src++)
len++;
return len;
}
strcpy
char *mystrcpy(char *dest,const char *src)
{
char *d = dest;
while(*dest++ = *src++);
return d;
}
strcat
char *mystrcat(char *dest,const char *src)
{
char *d = dest;
while(*dest) dest++;
while(*dest++ = *src++);
return d;
}
strcmp
int mystrcmp(char *dest,const char *src)
{
for(;(*src && *dest)&&(*src++ == *dest++););
return *dest - *src;
}
测试代码
int main()
{
char *p = "china";
char *q = "is great";
printf("%d\n",mystrlen(p));
char buf[10];
printf("%s\n",mystrcpy(buf,p));
printf("%s\n",mystrcat(p,q));
if(mystrcmp(p,q) != 0)
printf("!=\n");
else
printf("==");
return 0;
}