鱼C论坛

 找回密码
 立即注册
查看: 1876|回复: 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
#include <stdio.h>

int main()
{
        int count = 0;

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

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

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

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

int main()
{
        int count = 0;

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

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

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

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

int main()
{
        int count = 0;
        char buff[1024];

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

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

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-22 13:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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