鱼C论坛

 找回密码
 立即注册
查看: 589|回复: 2

[已解决]求c语言大佬教我一下c语言

[复制链接]
发表于 2020-4-28 23:13:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
用指针的方法编写程序,完成如下函数的功能:
int mystrcmp(char *str1,char *str2)——比较两个字符串str1,str2。若str1>str2,返回正数1;str1=str2,返回0;str1<str2,返回负数-1。(注:不能用字符串处理函数strcmp)
求大佬教教这道题怎么做!!!!!
最佳答案
2020-4-28 23:25:27
本帖最后由 sunrise085 于 2020-4-28 23:40 编辑
  1. #include<stdio.h>
  2. #define MAX 1024
  3. int mystrcmp(char *str1,char *str2){
  4.     int i=0;
  5.     while(str1[i]!='\0'&&str2[i]!='\0')
  6.     {
  7.         if (str1[i] - str2[i]>0)
  8.             return 1;
  9.         else if (str1[i] - str2[i]<0)
  10.             return -1;
  11.         i++;
  12.     }
  13.     return 0;
  14. }
  15. int main()
  16. {
  17.         char str1[MAX];
  18.         char str2[MAX];
  19.         int i = 0;
  20.         unsigned int n;

  21.         printf("请输入第一个字符串:");
  22.         while ((str1[i++] = getchar()) != '\n');

  23.         printf("请输入第二个字符串:");
  24.         i = 0;
  25.         while ((str2[i++] = getchar()) != '\n');

  26.         printf("比较的结果是:%d\n", mystrcmp(str1,str2));

  27.         return 0;
  28. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-28 23:25:27 | 显示全部楼层    本楼为最佳答案   
本帖最后由 sunrise085 于 2020-4-28 23:40 编辑
  1. #include<stdio.h>
  2. #define MAX 1024
  3. int mystrcmp(char *str1,char *str2){
  4.     int i=0;
  5.     while(str1[i]!='\0'&&str2[i]!='\0')
  6.     {
  7.         if (str1[i] - str2[i]>0)
  8.             return 1;
  9.         else if (str1[i] - str2[i]<0)
  10.             return -1;
  11.         i++;
  12.     }
  13.     return 0;
  14. }
  15. int main()
  16. {
  17.         char str1[MAX];
  18.         char str2[MAX];
  19.         int i = 0;
  20.         unsigned int n;

  21.         printf("请输入第一个字符串:");
  22.         while ((str1[i++] = getchar()) != '\n');

  23.         printf("请输入第二个字符串:");
  24.         i = 0;
  25.         while ((str2[i++] = getchar()) != '\n');

  26.         printf("比较的结果是:%d\n", mystrcmp(str1,str2));

  27.         return 0;
  28. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-28 23:40:56 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <string.h>

  3. int mystrcmp(char * str1 , char * str2)
  4. {
  5.         int ret = * str1 - * str2                                       ;
  6.         while(* str1 && * str2) if((ret = * str1 ++ - * str2 ++)) break ;
  7.         if(ret) ret = (ret > 0) ? 1 : -1                                ;
  8.         return ret                                                      ;
  9. }

  10. main(void)
  11. {
  12.         char s1[260] , s2[260]                                 ;
  13.         strcpy(s1 , "12345")                                   ;
  14.         strcpy(s2 , s1)                                        ;
  15.         printf("strcmp(s1 , s2) = %d\n" , strcmp(s1 , s2))     ;
  16.         printf("mystrcmp(s1 , s2) = %d\n" , mystrcmp(s1 , s2)) ;
  17.         strcpy(s2 , "12A45")                                   ;
  18.         printf("strcmp(s1 , s2) = %d\n" , strcmp(s1 , s2))     ;
  19.         printf("mystrcmp(s1 , s2) = %d\n" , mystrcmp(s1 , s2)) ;
  20.         strcpy(s2 , s1)                                        ;
  21.         strcpy(s1 , "ABCDE")                                   ;
  22.         printf("strcmp(s1 , s2) = %d\n" , strcmp(s1 , s2))     ;
  23.         printf("mystrcmp(s1 , s2) = %d\n" , mystrcmp(s1 , s2)) ;
  24. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 07:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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