鱼C论坛

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

按照小甲鱼给的代码在code blokcs编译结果有误

[复制链接]
发表于 2022-2-11 15:49:38 | 显示全部楼层 |阅读模式

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

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

x
#include <stdio.h>
#define MAX 1024

int main()
{
    char str[MAX];
    char *target = str;
    char ch;
    int length = 0;

    printf("请输入一个字符串:");
    fgets(str, MAX, stdin);

    while (1)
    {
            ch = *target++;
            if (ch == '\0')
            {
                    break;
            }
            if ((int)ch < 0)
            {
                    target += 2;
            }
            length++;
    }

    printf("您总共输入了 %d 个字符!\n", length);

    return 0;
}
从输入四个及以上中文字符开始,字符数就与实际输入字符数不一致(输入7个中文字符,输出时给出的是5)
2.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-11 16:21:30 | 显示全部楼层
请输入一个字符串:我和我的祖国,一刻也不能分割
您总共输入了 14 个字符!

--------------------------------
Process exited after 21.07 seconds with return value 0
请按任意键继续. . .

  1. #include <stdio.h>
  2. #define MAX 1024

  3. int main()
  4. {
  5.     char str[MAX];
  6.     char *target = str;
  7.     char ch;
  8.     int length = 0;

  9.     printf("请输入一个字符串:");
  10.     fgets(str, MAX, stdin);  //截至符是 /n

  11.     while (1)
  12.     {
  13.            
  14.             ch = *target++;
  15.             if (ch == '\n')
  16.             {
  17.                     break;
  18.             }
  19.             if ((int)ch < 0) //判断中文
  20.             {
  21.                     target += 1;
  22.             }
  23.             length++;
  24.     }

  25.     printf("您总共输入了 %d 个字符!\n", length);

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-24 10:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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