统计短文的单词数量

最近在家收拾笔记本,发现一篇手抄英文短文,不禁读了起来。短文故事生动,寓意美好。于是想编写一个统计单词数量的代码,留作纪念。


统计 N 个段落的英文短文的单词总数量。

输入格式:

输入在第一行给出 N。随后 N行给出短文的 N个段落。

输出格式:

在一行中输出单词总数量。缩写单词算作一个单词。

输入样例:

4
What are the basic element of good manners? Certainly a strong sense of justice is one; modesty is often nothing more than a highly developed sense of fair play. A friend of mine once told me about a time he was driving along a narrow, unpaved moutain road. Ahead was another car that produced clouds of dust, and it was a long way to the nearest paved highway. Suddenly, at a wider place, the car ahead pulled off the road. Thinking that its owner might have engine trouble, my friend stopped and asked if anything was wrong. "No", said the other driver, "but you've tolerated my dust this far; I'll catch up with yours the rest of the way."
Another element of courtesy is considerate, a quality that enables a person to see into the mind or heart of someone else, to understand the pain or unhapppiness there and to do something to minimize it. A man dining alone in a restaurant was trying to open the cap of a beer bottle, but he couldn't do it because of badly injured fingers. He asked a young busboy to help him. The boy took the bottle, turned his back momentarily and loosened the cap without difficulty. Then he tightened it again. Turning back to the man, he pretended to make great efforts to open the bottle without success. Finally he took it into the kitchen and returned shortly, saying that he had managed to loosen it -- but only with a pair of pliers.
Yet another element of politeness is the ability to treat all people alike, in spite of all status or importance. Even when you have doubts about some people, act as if they are worthy of your best manners. You may also be astonished to find out what they really are.
Courtesy is the key to a happier world.

输出样例:

310

代码:

#include <stdio.h>
#include <string.h>

int main()
{
	int N, i, sum, swi;
	char prg[10000], *p;
	
	scanf("%d\n", &N);
	
	for ( i=0,sum=0; i<N; i++ ) {
		
		gets(prg);
		
		for ( p=prg,swi=0; *p!='\0'; p++ ) {
			if ( swi ) {
				if ( *p==' ' ) {
					sum++;
					swi = 0;
				}
			} else {
				if ( isalpha(*p) ) {
					swi = 1;
				}
			}
		}
		
		if ( swi ) sum++;
		
	}
	
	printf("%d", sum);
	
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值