有戏

 找回密码
 立即注册
简历下载
搜索
热搜: 活动 交友 discuz
查看: 670|回复: 0

编写程序,不用strcmp函数,比较两个字符串的大小。

[复制链接]

1000

主题

1002

帖子

25万

积分

论坛元老

Rank: 8Rank: 8

积分
251951
发表于 2015-1-17 16:52:07 | 显示全部楼层 |阅读模式
*/
#include <stdio.h>
#include <string.h>
int main()
{
    char name1[40];
    char name2[40];
    printf("please input name1 and name2:\n");
    gets(name1);
    scanf("%s",name2);
  //  if(name1 == name2)
  //      printf("ok1!!!!");
//   int result = strcmp(name1,name2);

   /*
   name1 abc
   name2 abcd


   abc\0
   abcd

   int i=0;
   while( name1[i] == name2[i]  &&  name1[i]!= '\0' && name2[i]!= '\0')
   {
        i++;
   }

   if(name1[i] > name2[i])


   */
   int i=0;
   int result;
   while( name1[i] == name2[i]  &&  name1[i]!= '\0' && name2[i]!= '\0')
   {
        i++;
   }

   if(name1[i] > name2[i])
         result  = 1;
   else if(name1[i] < name2[i])
         result = -1;
   else
        result = 0;

    if( result == 0 )
        printf(" %s = %s\n", name1,name2);
    else if (result > 0)
        printf(" %s > %s ===> %d \n", name1,name2,result);
    else
        printf(" %s < %s ===> %d \n", name1,name2,result);
    return 0;
}
大家好
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|有戏 粤ICP备2020111303号

GMT+8, 2025-12-6 14:47 , Processed in 0.077475 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表