C语言:实现strcat函数,strcmp函数

实现strcat函数

#include

void myputs(char *p)
{
        while(*p != '\0')
        {
                printf("%c",*p++);
        }
        putchar('\n');
}

void myStrcat(char *dest,char *src)
{
        while(*dest != '\0')
        {
                dest++;
        }
        while(*src != '\0')
        {
                *dest++ = *src++;
        }
        *dest = '\0';
}

int main()
{
        char dest[128] = "99999";//后面会有123个'\0'
        char *src = "landlsad ad ";

        myStrcat(dest,src);
        myputs(dest);
        return 0;
}

实现strcmp函数

#include

int myStrcmp(char *str1,char *str2)
{
        int t;

        while((*str1 != '\0')  && (*str1 == *str2))
        {
                str1++;
                str2++;
        }
        //执行完while后,str1偏移到了4后面,为空,str2偏移到了5不为空,所以t<0
        t = *str1 - *str2;

        if(t == 0) return 0;
        if(t < 0)  return -1;
        if(t > 0)  return 1;
}



int main()
{
        char dest1[] = "1234";
        char dest2[] = "12345";

        int i = myStrcmp(dest1,dest2);
        printf("i=%d\n",i);
        return 0;
}

你可能感兴趣的:(C语言)