句子读单词

在一行中输入一个英文句子(不超过100个字符),输出这个句子中单词的个数,单词之间以空格分隔,除空格外都认为是单词(包括符号)。

输入样例:

This is a C program. <<< =22= , END
输出样例:

9

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
    char str[100];
    gets(str);
    int i=0,cnt=0,isword=0;
    while(str[i]!='\0')
    {
        while(str[i]==' ')
        {
            i++;
        }
        if(str[i]!='\0')
            isword=1;
        while(str[i]!=' '&&str[i]!='\0')
        {
            i++;
        }
        if(isword==1)
        {
            cnt++;
            isword=0;
        }
    }
    printf("%d",cnt);
}	

输入长度不超过80的英文文本,统计该文本中长度为n的单词总数(单词之间只有一个空格)。

输入格式:

首先输入一个正整数T,表示测试数据的组数,然后是T组测试数据。

每组数据首先输入1个正整数n(1≤n≤50),然后输入1行长度不超过80的英文文本(只含英文字母和空格)。注意:不要忘记在输入一行文本前吸收换行符。

输出格式:

对于每组测试数据,输出长度为n的单词总数。

输入样例:

2

5

hello world

5

acm is a hard game

输出样例:

2

0

c 复制代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t>0)
    {
        int n;
        scanf("%d",&n);
        getchar();
        char str[100];
        gets(str);
        int i=0,cnt=0,isword=0;
        while(str[i]!='\0')
        {
            int wordlen=0;
            while(str[i]==' ')
            {
                i++;
            }
            if(str[i]!='\0')
                isword=1;
            while(str[i]!=' '&&str[i]!='\0')
            {
                i++;
                wordlen++;
            }
            if(wordlen==n)
            {
                cnt++;
            }
            if(isword==1)
            {
                isword=0;
            }
        }
        printf("%d\n",cnt);
        t--;
    }
    
}
相关推荐
jarreyer16 小时前
【算法记录1】模型训练问题
算法
Felven16 小时前
D. Friends and the Restaurant
算法
想吃火锅100516 小时前
【leetcode】165.比较版本号js
javascript·算法·leetcode
San813_LDD16 小时前
[量化]《浮点数比较的艺术:从内存布局到极致性能优化》
网络·算法
ysu_031416 小时前
leetcode数据结构与算法1~4
c语言·数据结构·学习·算法·leetcode
小欣加油16 小时前
leetcode2574 左右元素和的差值
数据结构·c++·算法·leetcode·职场和发展
PH = 716 小时前
动态规划-求最优解-自底向上
算法·动态规划
用户4978630507317 小时前
前缀和与差分
算法
weixin_4617694017 小时前
通过数组和队列构造二叉树方法(用于算法测试),C++ vector不能直接使用null
数据结构·c++·算法·vector·nullptr·null
caimouse17 小时前
Reactos 第 4 章 对象管理 — 4.1 对象与对象目录
服务器·c语言·开发语言·windows·架构