鱼C论坛

 找回密码
 立即注册
查看: 2430|回复: 6

[已解决]S1E19动动手写strcmp函数

[复制链接]
发表于 2022-8-8 18:50:38 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<stdio.h>
  2. int main()
  3. {
  4.         char str1[100],str2[100];
  5.         printf("Please input the first string:");
  6.         scanf("%s",&str1);
  7.         printf("Please input the second string:");
  8.         scanf("%s",&str2);
  9.        
  10.         signed int i;
  11.        
  12.         printf("Please input the number you'd like to compare:");
  13.         scanf("%d",&i);
  14.        
  15.         if (str1[i]==str2[i])
  16.         {
  17.                 printf("The result is 0!\n");
  18.     }
  19.         else
  20.         {
  21.                 printf("The result is %d\n",str1[i]-str2[i]);
  22.         }

  23.         return 0;
  24. }
复制代码

如果输入的字符串中间有空格就出错,为什么?
最佳答案
2022-8-8 19:05:25
本帖最后由 一点点儿 于 2022-8-9 15:44 编辑

scanf扫描到空格时,就认为对str的赋值结束,并忽略了后面的字符串。注意:后面的字符串依然还在stdin缓冲区。

怎么才能解决这样的问题呢?

可以用gets()代替scanf():scanf("%s",&str1)改为gets(str1)
                              
                                     scanf("%s",&str2)改为gets(str2)

如下:

  1. #include <stdio.h>

  2. int main() {
  3.         char str1[100], str2[100];
  4.         printf("Please input the first string:");
  5.         gets(str1);
  6.         printf("Please input the second string:");
  7.         gets(str2);

  8.         signed int i;

  9.         printf("Please input the number you'd like to compare:");
  10.         scanf("%d", &i);

  11.         if (str1[i] == str2[i]) {
  12.                 printf("The result is 0!\n");
  13.         } else {
  14.                 printf("The result is %d\n", str1[i] - str2[i]);
  15.         }

  16.         return 0;
  17. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-8-8 19:05:25 | 显示全部楼层    本楼为最佳答案   
本帖最后由 一点点儿 于 2022-8-9 15:44 编辑

scanf扫描到空格时,就认为对str的赋值结束,并忽略了后面的字符串。注意:后面的字符串依然还在stdin缓冲区。

怎么才能解决这样的问题呢?

可以用gets()代替scanf():scanf("%s",&str1)改为gets(str1)
                              
                                     scanf("%s",&str2)改为gets(str2)

如下:

  1. #include <stdio.h>

  2. int main() {
  3.         char str1[100], str2[100];
  4.         printf("Please input the first string:");
  5.         gets(str1);
  6.         printf("Please input the second string:");
  7.         gets(str2);

  8.         signed int i;

  9.         printf("Please input the number you'd like to compare:");
  10.         scanf("%d", &i);

  11.         if (str1[i] == str2[i]) {
  12.                 printf("The result is 0!\n");
  13.         } else {
  14.                 printf("The result is %d\n", str1[i] - str2[i]);
  15.         }

  16.         return 0;
  17. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-8-8 19:07:10 | 显示全部楼层
  1. #include <stdio.h>

  2. int main() {
  3.         char str1[100], str2[100];
  4.         printf("Please input the first string:");
  5.         gets(str1);
  6.         printf("Please input the second string:");
  7.         gets(str2);

  8.         signed int i;

  9.         printf("Please input the number you'd like to compare:");
  10.         scanf("%d", &i);

  11.         if (str1[i] == str2[i]) {
  12.                 printf("The result is 0!\n");
  13.         } else {
  14.                 printf("The result is %d\n", str1[i] - str2[i]);
  15.         }

  16.         return 0;
  17. }
复制代码

这样
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-8 19:40:23 | 显示全部楼层

作为 C 语言里少见的被直接删除的函数,个人认为 gets 还是能不用就不用。可以使用 fgets 之类的函数进行代替。
(偏题 这个代码的功能好像和我认识的 strcmp 不太一样...)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-8-9 16:46:27 | 显示全部楼层

谢谢,还有一个问题,最后得出的结果是用两个字符的什么相减?去查了ASCII字符表发现得出的结果与字符对应的十进制数并不相等
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-9 17:03:47 | 显示全部楼层
zyxmm 发表于 2022-8-9 16:46
谢谢,还有一个问题,最后得出的结果是用两个字符的什么相减?去查了ASCII字符表发现得出的结果与字符对 ...

最后得出的结果是用两个字符对应的ASCII码十进制数相减
我运行得出的结果没错,
str[]是从0开始索引的,
你会不会是按照str[]从1开始索引验证结果的?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-8-9 17:22:37 | 显示全部楼层
一点点儿 发表于 2022-8-9 17:03
最后得出的结果是用两个字符对应的ASCII码十进制数相减
我运行得出的结果没错,
str[]是从0开始索引的 ...

对,我从1开始索引的,按0开始结果就对了,谢谢

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-17 00:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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