C++ - 实现strcmp函数

版权声明:欢迎转载并请注明出处,谢谢~~ https://blog.csdn.net/chimomo/article/details/7716284
/*
 * Created by Chimomo
 */

#include <iostream>

using namespace std;

class String {
public:
    static int Compare(const char *str1, const char *str2) {
        if (str1 == NULL || str2 == NULL) {
            if (str1 == NULL && str2 != NULL) {
                return -*str2;
            }
            if (str1 != NULL && str2 == NULL) {
                return *str1;
            }
            if (str1 == NULL && str2 == NULL) {
                return 0;
            }
        } else {
            while (*str1 && *str2 && *str1++ == *str2++);
            return *str1 - *str2;
        }
    }
};

int main() {
    char str1[10] = "456456789";
    char *str2 = NULL;
    cout << String::Compare(str1, str2) << endl;
    return 0;
}

// Output:
/*
52

*/

猜你喜欢

转载自blog.csdn.net/chimomo/article/details/7716284