为了账号安全,请及时绑定邮箱和手机立即绑定

想要实现两个字符串的比较有会写吗?

想要实现两个字符串的比较有会写吗?

C
当年话下 2022-11-18 17:13:37
Description编写函数strcomp(char*s1, char *s2),实现两个字符串的比较,返回值为1、0或-1,分别表示s1>s2,s1=s2,s1<>Input多组测试数据,每组输入两个字符串(字符串长度小于80)。Output根据字符串的大小关系,输出1、0或-1Sample InputchinachineseworldhelloseaseaSample Output-110
查看完整描述

2 回答

?
噜噜哒

TA贡献1784条经验 获得超7个赞

#include <stdio.h>

int strocmp(char* s1, char *s2)
{
int i;
for(i = 0; s1[i] && s2[i]; i++) {
if(s1[i] < s2[i])
return -1;
else if(s1[i] > s2[i])
return 1;
}
if(s1[i] < s2[i])
return -1;
else if(s1[i] > s2[i])
return 1;
return 0;
}

main()
{
char s1[32], s2[32];

while(1) {
gets(s1);
if(s1[0] == 0)
break;
gets(s2);
printf("%d\n", strocmp(s1, s2));
}
}

查看完整回答
反对 回复 2022-11-22
?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

int strcomp(char* s1, char* s2){    int nRet = *s1 - *s2;    if (nRet)    {        if (nRet > 0)            return 1;        else            return -1;    }    else    {        if (!*s1 && !*s2)        {            return 0;        }        strcomp(s1 + 1, s2 + 1);    }}

查看完整回答
反对 回复 2022-11-22
  • 2 回答
  • 0 关注
  • 76 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信