鱼C论坛

 找回密码
 立即注册
查看: 2899|回复: 4

[已解决]关于strncmp

[复制链接]
发表于 2018-11-6 16:53:16 | 显示全部楼层 |阅读模式
5鱼币
大家号,为什么程序运行后没有结果呢?
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>

  4. #define MAX 1024
  5. int main()
  6. {
  7.         char str1[MAX];
  8.         char str2[MAX];
  9.         int n,m,i=0;
  10.        
  11.         printf("请输入第一个字符串:\n");
  12.         while ((str1[i++] = getchar()) != '\n');
  13.        
  14.         printf("请输入第二个字符串:\n");
  15.         while ((str2[i++] = getchar()) != '\n');
  16.        
  17.         printf("请输入要比较的位数:\n");
  18.         scanf("%d",&n);
  19.        
  20.         if (!strncmp(str1, str2, n))
  21.         {printf("两字符串前%d个字符相同",n);}
  22.        
  23.         system("pause");
  24.         return 0;
  25.         }
复制代码


屏幕截图(44).png
最佳答案
2018-11-6 16:53:17
不好意思,我看错了。你第二个输入i,没有归0;
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>

  4. #define MAX 1024
  5. int main()
  6. {
  7.         char str1[MAX];
  8.         char str2[MAX];
  9.         int n,m,i=0;
  10.         
  11.         printf("请输入第一个字符串:\n");
  12.         while ((str1[i++] = getchar()) != '\n');
  13.         
  14.         i = 0;

  15.         printf("请输入第二个字符串:\n");
  16.         while ((str2[i++] = getchar()) != '\n');
  17.         
  18.         printf("请输入要比较的位数:\n");
  19.         scanf("%d",&n);
  20.         
  21.         if (!strncmp(str1, str2, n))
  22.         {printf("两字符串前%d个字符相同",n);}
  23.         
  24.         system("pause");
  25.         return 0;
  26.         }
复制代码

最佳答案

查看完整内容

不好意思,我看错了。你第二个输入i,没有归0;
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-11-6 16:53:17 | 显示全部楼层    本楼为最佳答案   
不好意思,我看错了。你第二个输入i,没有归0;
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>

  4. #define MAX 1024
  5. int main()
  6. {
  7.         char str1[MAX];
  8.         char str2[MAX];
  9.         int n,m,i=0;
  10.         
  11.         printf("请输入第一个字符串:\n");
  12.         while ((str1[i++] = getchar()) != '\n');
  13.         
  14.         i = 0;

  15.         printf("请输入第二个字符串:\n");
  16.         while ((str2[i++] = getchar()) != '\n');
  17.         
  18.         printf("请输入要比较的位数:\n");
  19.         scanf("%d",&n);
  20.         
  21.         if (!strncmp(str1, str2, n))
  22.         {printf("两字符串前%d个字符相同",n);}
  23.         
  24.         system("pause");
  25.         return 0;
  26.         }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-11-7 18:02:35 | 显示全部楼层
由于缓存问题,str2存入了\n
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-11-7 18:08:18 | 显示全部楼层
千公子 发表于 2018-11-7 18:07
不好意思,我看错了。你第二个输入i,没有归0;

希望采纳,第一次回答问题!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-11-7 22:50:44 | 显示全部楼层
千公子 发表于 2018-11-6 16:53
不好意思,我看错了。你第二个输入i,没有归0;

谢谢啊
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-22 21:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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