句子读单词

在一行中输入一个英文句子(不超过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--;
    }
    
}
相关推荐
ZoeJoy819 分钟前
算法筑基(二):搜索算法——从线性查找到图搜索,精准定位数据
算法·哈希算法·图搜索算法
Alicx.24 分钟前
dfs由易到难
算法·蓝桥杯·宽度优先
_日拱一卒35 分钟前
LeetCode:找到字符串中的所有字母异位词
算法·leetcode
云泽8081 小时前
深入 AVL 树:原理剖析、旋转算法与性能评估
数据结构·c++·算法
Wilber的技术分享2 小时前
【LeetCode高频手撕题 2】面试中常见的手撕算法题(小红书)
笔记·算法·leetcode·面试
邪神与厨二病2 小时前
Problem L. ZZUPC
c++·数学·算法·前缀和
梯度下降中3 小时前
LoRA原理精讲
人工智能·算法·机器学习
IronMurphy3 小时前
【算法三十一】46. 全排列
算法·leetcode·职场和发展
czlczl200209253 小时前
力扣1911. 最大交替子序列和
算法·leetcode·动态规划
靴子学长4 小时前
Decoder only 架构下 - KV cache 的理解
pytorch·深度学习·算法·大模型·kv