液氢 2019-11-10 20:37 采纳率: 100%
浏览 1281
已采纳

c语言不用函数实现strcmp()输入相同字符串时比较不正确

c语言不用函数实现strcmp()输入相同字符串时比较不正确

//strcmp函数
#include<stdio.h>
void main()
{
    int i=0,re,m,n;
    char str1[1000],str2[1000];
    printf("请输入字符串str1:");
    gets(str1);
    printf("请输入字符串str2:");
    gets(str2);
    do
    {
        i++;
        if(str1[i]==str2[i])
            {
                re=0;
                continue;
            }
        else if(str1[i]>str2[i])
            {
                re=1;
                break;
            }
        else if(str1[i]<str2[i])
            {
                re=-1;
                break;
            }
    }while(i<1000);
    printf("%d\n",i);
    printf("%d\n",re);
    if(re==1)
        printf("字符串str1大于字符串str2");
    if(re==-1)
        printf("字符串str1小于字符串str2");
    if(re==0)
        printf("字符串str1等于字符串str2");

}

图片说明

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-11-10 21:20
    关注
    #include<stdio.h>
    void main()
    {
        int i=0,re=0,m,n;
        char str1[1000],str2[1000];
        printf("str1:");
        gets(str1);
        printf("str2:");
        gets(str2);
        do
        {
            if (!str1[i] && !str2[i])
                {
                    re=0;
                    break;
                }
            else if(str1[i]>str2[i] || !str2[i])
                {
                    re=1;
                    break;
                }
            else if(str1[i]<str2[i] || !str1[i])
                {
                    re=-1;
                    break;
                }
            i++;
        }while(i<1000);
        printf("%d\n",i);
        printf("%d\n",re);
        if(re==1)
            printf(">");
        if(re==-1)
            printf("<");
        if(re==0)
            printf("==");
    
    }
    

    问题解决请采纳

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程