鱼C论坛

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

[已解决]getchar问题

[复制链接]
发表于 2021-2-26 15:09:10 | 显示全部楼层 |阅读模式
1鱼币
#include <stdio.h>

int main()
{
        int count = 0;

        printf("请随意一个英文句子:");

        while (getchar() != '\n')
        {
                count = count + 1;
        }

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

        return 0;
}
这个如果想知道包括回车键的话要怎么改?
还有如果用scanf来写的话怎么写?
最佳答案
2021-2-26 15:09:11
  1. #include <stdio.h>

  2. int main()
  3. {
  4.         int count = 0;

  5.         printf("请随意一个英文句子:");

  6.         while (getchar() != '\n')
  7.         {
  8.                 count = count + 1;
  9.         }

  10.         //printf("你总共输入了%d个字符!\n", count);
  11.         printf("你总共输入了%d个字符!\n", count + 1);

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

使用道具 举报

发表于 2021-2-26 15:09:11 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>

  2. int main()
  3. {
  4.         int count = 0;

  5.         printf("请随意一个英文句子:");

  6.         while (getchar() != '\n')
  7.         {
  8.                 count = count + 1;
  9.         }

  10.         //printf("你总共输入了%d个字符!\n", count);
  11.         printf("你总共输入了%d个字符!\n", count + 1);

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

使用道具 举报

发表于 2021-2-26 15:21:11 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main()
  4. {
  5.         int count = 0;
  6.         char buff[1024];

  7.         printf("请随意一个英文句子:");
  8.         scanf("%s", buff);
  9.         count = strlen(buff);

  10.         //printf("你总共输入了%d个字符!\n", count);
  11.         printf("你总共输入了%d个字符!\n", count + 1);

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-1 15:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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