不使用库函数,编写函数int strcmp(char *source, char *dest) 相等返回0,不等返回-1;

#include



int strcmp(char *source, char *dest)
{

    int m,n,i,flag;
    m = strlen(source);
    n = strlen(dest);
    if(m!=n)
        flag = -1;

    if(source==NULL&&dest==NULL)
    {
        flag = 0;
    }


    if(source!=NULL&&dest!=NULL)
    {
        while(*source==*dest&&*source!='\0')
        {
             source++;dest++;
        }
        if(*dest=='\0')
            flag = 0;
        else
            flag = -1;

    }
    else
        flag = -1;

    return flag;
}

void main()
{
    int i;
    char *str = "hello";
    char *str1 = "hello";
    i = strcmp(str,str1);
    printf("%d",i);

}

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