鱼C论坛

 找回密码
 立即注册
查看: 2757|回复: 1

[已解决]S1e22第三题求助

[复制链接]
发表于 2022-10-30 21:04:25 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 Khayal 于 2022-10-30 21:04 编辑

实现 strncmp 函数,允许用户指定前 n 个字符进行对比,这一次要求支持中英文混合的字符串比较噢!

我写的如下:
#include <stdio.h>

#define MAX 1024

int main()
{
        char str1[MAX], str2[MAX];

        char *target1 = str1, *target2 = str2;

        int index = 1, n;

        printf("Input the first string:");
        fgets(str1, MAX, stdin);

        printf("Input the secong string:");
        fgets(str2, MAX, stdin);

        printf("Input the number for comparing:");
        scanf("%d", &n);

        while (n && *target1 != '\0' && *target2 != '\0')
        {
            if (*target1++ != *target2++)
            {
                   break;
            }
                        index++;
            n--;
        }

        if ((n == 0) || (*target1 == '\0' && *target2 == '\0'))
        {
                printf("The same!\n", index);
        }
        else
        {
                printf("Different in the %d char!\n", index);
        }

        return 0;
}

Q1:小甲鱼在while这里将中文的情况区分开来,即
  1. char ch;
  2. ……
  3. while (n && *target1 != '\0' && *target2 != '\0')
  4.         {
  5.                 ch = *target1;
  6.                 if (ch < 0)
  7.                 {
  8.                         if (*target1++ != *target2++ || *target1++ != *target2++)
  9.                         {
  10.                                 break;
  11.                         }
  12.                 }
  13.                 if (*target1++ != *target2++)
  14.                 {
  15.                        break;
  16.                 }
  17.                 index++;
  18.                 n--;
  19.         }
复制代码

但我看不用区分ch<0的情况也能成功?

Q2:如果我把n处写成这样:
while (n-- && *target1 != '\0' && *target2 != '\0')
F(@WG@}T148FHW7(M57[~3S.jpg
而这个问题只会在最后一个字符不同时出现

求助大佬!
最佳答案
2022-10-30 21:46:07
  1. #include <stdio.h>

  2. #define MAX 1024

  3. int strncmp(char * s1 , char * s2 , int n)
  4. {
  5.         char * p1 , * p2                                               ;
  6.         int i , r                                                      ;
  7.         for(i = 0 , p1 = s1 , p2 = s2 ; * p1 && * p2 && i < n ; p1 ++ , p2 ++ , i ++) {
  8.                 r = * p1 - * p2                                        ;
  9.                 if(* p1 < 0) {
  10.                         r = (* p1 - * p2) || (* (p1 + 1) - * (p2 + 1)) ;
  11.                         p1 ++                                          ;
  12.                         p2 ++                                          ;
  13.                 }
  14.                 if(r) break                                            ;
  15.         }
  16.         return r                                                       ;
  17. }

  18. int main()
  19. {
  20.         int n                                                          ;
  21.         char str1[MAX] , str2[MAX]                                     ;
  22.         printf("Input the first string:")                              ;
  23.         fgets(str1, MAX, stdin)                                        ;

  24.         printf("Input the secong string:")                             ;
  25.         fgets(str2, MAX, stdin)                                        ;

  26.         printf("Input the number for comparing:")                      ;
  27.         scanf("%d", &n)                                                ;

  28.         if(! strncmp(str1 , str2 , n)) printf("The same!\n")           ;
  29.         else printf("the difference!\n")                               ;
  30. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-10-30 21:46:07 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>

  2. #define MAX 1024

  3. int strncmp(char * s1 , char * s2 , int n)
  4. {
  5.         char * p1 , * p2                                               ;
  6.         int i , r                                                      ;
  7.         for(i = 0 , p1 = s1 , p2 = s2 ; * p1 && * p2 && i < n ; p1 ++ , p2 ++ , i ++) {
  8.                 r = * p1 - * p2                                        ;
  9.                 if(* p1 < 0) {
  10.                         r = (* p1 - * p2) || (* (p1 + 1) - * (p2 + 1)) ;
  11.                         p1 ++                                          ;
  12.                         p2 ++                                          ;
  13.                 }
  14.                 if(r) break                                            ;
  15.         }
  16.         return r                                                       ;
  17. }

  18. int main()
  19. {
  20.         int n                                                          ;
  21.         char str1[MAX] , str2[MAX]                                     ;
  22.         printf("Input the first string:")                              ;
  23.         fgets(str1, MAX, stdin)                                        ;

  24.         printf("Input the secong string:")                             ;
  25.         fgets(str2, MAX, stdin)                                        ;

  26.         printf("Input the number for comparing:")                      ;
  27.         scanf("%d", &n)                                                ;

  28.         if(! strncmp(str1 , str2 , n)) printf("The same!\n")           ;
  29.         else printf("the difference!\n")                               ;
  30. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 08:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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