搜尋此網誌

[C/C++][實作strcmp]

/*
* Author: MH
* Since 2017/11/24
* Toolkit: Code::Block 16.01
*/

int strcmp_implementation(char *source, char *dest)
{
    while (*source!='\0' || *dest!='\0')
    {
        if (*source!=*dest)
            return -1;
        source++, dest++;
    }
    return 0;
}

int main()
{
    char *source = "abc";
    char *dest1 = "abc";
    char *dest2 = "abd";

    printf("ret1: %d\n", strcmp_implementation(source, dest1));    // return 0
    printf("ret2: %d\n", strcmp_implementation(source, dest2));    // return -1

    system("pause");
    return 0;
}

沒有留言:

張貼留言