《C Primer Plus》练习题:7.12编程练习

1、编写一个程序,该程序读取输入直到遇到#字符,然后报告读取的空格数目、读取的换行符数目以及读取的所有其他字符数目

void statistics()
{
	char c;
	int i = 0;
	int n_space = 0;
	int n_lines = 0;
	int n_others = 0;
	while ((c = getchar()) != '#') {
		if (c == ' ')
			n_space++;
		else if (c == '\n')
			n_lines++;
		else
			n_others++;
	}
	printf("n_space == %d\n", n_space);
	printf("n_lines == %d\n", n_lines);
	printf("n_others == %d\n", n_others);
}

2、编写一个程序,该程序输入直到遇到#字符。使程序打印每个输入的字符以及它的十进制ASCll码,每行打印8个字符/编码对。

void ASCllEncodingOfLetters()
{
	char ch;
	int i = 0;
	printf("请输入字符串:");
	while ((ch = getchar()) != '#')
	{
		printf(" %c:%d ", ch, ch);
		i++;
		if (i % 8 == 0) {
			printf("\n");
		}
	}
}

4、用if else语句编写程序读取的输入,直到#。利用一个感叹号代替每个句号,将原有的每个感叹号用两个感叹号代替,最后输出进行了多少次替换。

void main()
{
	char ch;
	int n = 0;
	printf("请输入字符串:");

	while ((ch = getchar()) != '#')
	{
		if (ch == '.') {
			putchar('!');
			n++;
		}
		else if (ch == '!')
		{
			putchar('!');
			putchar('!');
			n++;
		}
		else
			putchar(ch);
	}

	printf("共计进行了%d次替换\n", n);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值