C primer plus 第六版 第八章 编程练习 答案

本文介绍使用C语言处理字符流的方法,包括统计字符数、区分大小写字母数量及实现猜数字游戏。通过示例代码展示如何利用getchar()和islower/isupper函数进行字符读取与判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

8.1

//设计一个程序,统计在读到文件结尾之前读取的字符数

#include <stdio.h>	

int main(void)
{
	int count = 0;
	char ch;

	while ((ch = getchar()) != EOF)
		count++;
	printf("There are %d chars.", count);

	return 0;
}

8.2不懂

8.3

//把输入作为字符流读取,报告大写字母和小写字母的个数

#include <stdio.h>
#include <ctype.h>

int main(void)
{
	char ch = 0;
	int count_lower = 0;
	int count_upper = 0;

	while ((ch = getchar()) != EOF)
	{
		if (islower(ch))
			count_lower++;
		if (isupper(ch))
			count_upper++;
	}
	printf("The number of lower letter:%d , upper letter: %d.\n",
		count_lower, count_upper);

	return 0;
}

8.5

 #include <stdio.h>

char get_first(void);

int main(void)
{
	int guess = 50;
	int start = 1;
	int end = 100;
	char ch;

	printf("Please pick an integer from 1 to 100."
		"I will guess it.\n");
	printf("is it 50?\n");
	while ((ch = get_first()) != 'y')
	{
		printf("OK, is it biger or smaller than the guess? bigger or small?\n");
		if ('b' == (ch = get_first()))
		{
			end = guess;
			guess = (start + end) / 2;
		}
		else
		{
			start = guess;
			guess = (start + end) / 2;
		}
		printf("The number is %d,is it right?\n", guess);
	}

	printf("I guess it.");
}

char get_first(void) {
	int ch;
	ch = getchar();
	while (getchar() != '\n')
		continue;
	return ch;
}

8.6

有空再更

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值