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
有空再更