C++比较字符串大小(自己实现strcmp()函数)

详见:
https://www.nowcoder.com/practice/963e455fdf7c4a4a997160abedc1951b?tpId=225&tags=&title=&difficulty=0&judgeStatus=0&rp=0

1.移动指针法

但这种方法只适用于二个字符数组中从头开始,有几个相同字符的字符数组,不适用于其它情况。
时间复杂度:O(n),n为较短的字符串的长度
空间复杂度:O(1),只有指针,无额外空间

输入mystrcmp函数的是两个字符数组指针,我们对两个指针不断往后移,并比较找到第一个两个指针指向元素不同的地方,然后直接根据那个位置字符相减(对比二个字符)得到返回结果的正负或者0.

#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 && *dst && *src==*dst) {  //找到两个字符串第一个不同的字符
        src++;
        dst++;
    }

    if (*src > *dst)  //根据字符大小来返回.例如此时*src是字符`w`,*dsc为``空`,代表0个字符。则*src > *dst。
        return 1;
    else if (*src < *dst)
        return -1;
    else
        return 0;

}

2.循环

同上种,也只适用于二个字符数组中从头开始,有几个相同字符的字符数组

  • 通过循环逐一比较两字符串中对应位置每个字符的大小。
  • 如果src当前字符小于dst,说明src小于dst,返回-1。如果src当前字符大于dst,说明src大于dst,返回1。如果相等,则指针后移。
  • 如果src后面还有字符,返回1。如果dst后面还有字符,返回-1。如果都没有,返回0。

时间复杂度:假设两字符串长度为n和m,循环最多需要执行min(n,m)次,所以时间复杂度为O(min(n,m))。
空间复杂度:需要额外常数级别的空间,所以空间复杂度为O(1)

#include <iostream>
// #include <cstring>
#include <bits/stdc++.h>
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) {
    //计算src和dst长度
    int len_src = strlen(src);
    int len_dst = strlen(dst);
    int i=0,j=0; 
    while(i<len_src && j<len_dst) {
        //如果src当前字符小于dst,说明src小于dst,返回-1
        if (src[i]<dst[j]) {
            return -1;
        } 
        //如果src当前字符大于dst,说明src大于dst,返回1
        else if (src[i]>dst[j]) {
            return 1;
        } 
        //如果src当前字符等于dst,两指针后移
        else {
            i++;
            j++;
        }
            
    }
    //如果src后面还有字符,返回1
    if (i<len_src) {
        return 1;
    }
    //如果dst后面还有字符,返回-1
    else if (j<len_dst){
        return -1;
    }
    else{
        return 0;
    }
    return 0;
    
//     while ()
    
}

3.对比各个字符串(数组)的总ascii码(推荐这个)

首先明确是比较字符串大小而不是比较字符串长度,由于文中使用的是字符数组来存储字符串,所以可将字符串的每一个字符读取,得到ASCII码,分别加合起来在再比较就可解决该问题。

#include <iostream>
// #include <cstring>
#include <bits/stdc++.h>
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) {
    
    int len_src = strlen(src);
    int len_dst = strlen(dst);
    int sum_src=0,sum_dst=0;   
    for(int i=0; i<len_src; i++){
        sum_src += (int)src[i];
    }
    for(int i=0; i<len_dst; i++){
        sum_dst += (int)dst[i];
    }
    
    if(sum_src>sum_dst) 
        return 1;
    else if(sum_src<sum_dst) 
        return -1;
    else
        return 0;


}
  • 2
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值