题解 | #比较字符串大小#

比较字符串大小

https://www.nowcoder.com/practice/963e455fdf7c4a4a997160abedc1951b

    #include <iostream>
    using namespace std;

    int mystrcmp(const char* src, const char* dst);

    int main() {

        char s1[100] = { 0 };
        char s2[100] = { 0 };

        cin.getline(s1, sizeof(s1));
        cin.getline(s2, sizeof(s2));

        int ret = mystrcmp(s1, s2);

        cout << ret << endl;

        return 0;
    }

    int mystrcmp(const char* src, const char* dst) {

        while ((*src)!='\0'&&(*dst)!='\0') {            
            if ((*src)>(*dst)) {
                return 1;
            }
            else if ((*src)<(*dst)) {
                return -1;
            }
            dst++;
            src++;
        }//if same length

        if ((*src)==(*dst)) {
            return 0;
        }
        else if (*dst=='\0') {
            return 1;
        }
        else {
            return -1;
        }
        

    }

全部评论

相关推荐

今天 09:59
复旦大学 Java
点赞 评论 收藏
分享
12-13 14:51
已编辑
井冈山大学 算法工程师
龙虾x:算法比你强的没有你美,比你美的…..算了已经没有比你美的了
工作两年想退休了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务