统计单词 UESTC - 60

统计单词 UESTC - 60


Problem

编写一个函数,该函数能够统计一个英文字符串中有多少个单词。

Input

第一行是整数nn,表示测试的数据组数,下面是nn行含空格的字符串(仅由空格和英文字符组成,长度不超过200)。

Output

每行输入对应一行输出,表示对应行有多少单词。

Sample Input

1
I am a student

Sample Output

4

ps:字母+空格 肯定有一个单词,回车另算

代码如下:

#include<iostream>  
#include<cstdio>  
#include<cstring>  
#include<algorithm> 
#include<string.h> 
using namespace std; 
int main () {
    int t;
    scanf("%d", &t);
    getchar(); //消除回车影响 
    while( t-- ) {
        char str[210];
        gets(str);
        int len = strlen(str);
        int sum = 0;
        for(int i = 0; i <= len; i++) {
            if((str[i]>='a' && str[i]<='z')||(str[i]>='A' && str[i]<='Z'))
                continue;//判断第一个空格前一位是不是字母,存在多空格的情况 
            if((str[i-1]>='a' && str[i-1]<='z')||(str[i-1]>='A' && str[i-1]<='Z')||str[i-1]=='\n')
                sum++;
        }
        printf("%d\n", sum);
    }
    return 0;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值