the C programming language 练习

5 篇文章 0 订阅

p13 ,练习1-9

连续空格用一个代替

#include <stdio.h>

void main()
{
	int c = getchar();
	int lastc = 0;
	while (c != '\n')
	{
		if ((c!=' ') || (lastc!=' '))	//输出字符的条件集合
			putchar(c);
		lastc = c;
		c = getchar();
	}
	putchar('\n');
}


p15 ,练习1-12

每行一个单词输出

使用符号常量

#include <stdio.h>

#define IN 1
#define OUT 0

void main()
{
	int c = 0;
	int state = OUT;
	while ((c=getchar()) != '\n')
	{
		if (c==' ' || c=='\t')
		{
			if (state == IN)
			{
				putchar('\n');
				state = OUT;
			}
		}
		else if (state == OUT)
		{
			state = IN;
			putchar(c);
		}
		else 
			putchar(c);
	}
	putchar('\n');
}

p15~16 ,示例代码

统计各数字,空白符,其他字符数

多路判定:

if
else if
……
else
代码:

#include <stdio.h>

void main()
{
	int c = 0;
	int i = 0;
	int nwhite = 0;
	int nother = 0;
	int ndigit[10];
	for (i = 0; i < 10; i++)
		ndigit[i] = 0;

	while ((c = getchar()) != '\n')
	{
		if (c >= '0' && c <= '9')
			ndigit[c-'0']++;
		else if (c==' ' || c=='\t')
			nwhite++;
		else
			nother++;
	}

	printf("digit =");
	for (i = 0; i < 10; i++)
		printf(" %d", ndigit[i]);
	printf("\nwhite space = %d\nother = %d\n", nwhite, nother);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值