【C++】 应用指针,编写一个函数strcmp()实现两个字符串s1和s2的比较。如果s1=s2,则返回值为0,如果s1不等于s2,返回它们两者第一个不同的字符的ASC II码差值,并且若s1>s2,

#include<iostream>  
using namespace std;
int strlen(char str)
{
    int i=0, len = 0;
    while (str != '/0')
    {
        len++;
        i++;
    }
    return len;
}
int strcmp(char str1[], char str2[], int lenth)
{
    int i;
    char* p1,*p2;
    p1 = str1;
    p2 = str2;
    for ( i = 0; i < lenth; i++)
    {
        if (*(p1+i) != *(p2+i))
            break;
    }
    if (i == lenth - 1)
        return 0;
    else
        return((int)*(p1 + i)) - ((int)*(p2 + i));
}
int main()
{
    int lenth = 0;
    char str1[30] ;
    char str2[30];
    cout << "请输入字符串1和字符串2" << endl;
    cin >> str1 >> str2;
    if(strlen(str1)<strlen(str2))
        lenth= strlen(str1);
    else
        lenth= strlen(str2);
    if (strcmp(str1, str2, lenth) == 0)
        cout << "返回值为0" << endl;
    else
        if (strcmp(str1, str2, lenth) > 0)
           
            cout << "返回值为1" << endl;
        else
            cout << "返回值为-1" << endl;
    return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忘川Lethe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值