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

比较字符串大小

http://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) {

    // write your code here......
    int len1=sizeof(src)/sizeof(char),len2=sizeof(dst)/sizeof(char);
    int len=len1>len2?len1:len2;
    for(int i=0;i<len;i++){
        if(src[i]>dst[i]){
            return 1;
        }
        if(src[i]<dst[i])
            return -1;
    }
    return 0;

}
全部评论

相关推荐

评论
5
1
分享

创作者周榜

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