句子读单词

在一行中输入一个英文句子(不超过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--;
    }
    
}
相关推荐
励志的小陈3 小时前
贪吃蛇(C语言实现,API)
c语言·开发语言
知识浅谈4 小时前
DeepSeek V4 和 GPT-5.5 在同一天发布了??我也很懵,但对比完我悟了
算法
DeepModel4 小时前
通俗易懂讲透 Q-Learning:从零学会强化学习核心算法
人工智能·学习·算法·机器学习
田梓燊4 小时前
力扣:19.删除链表的倒数第 N 个结点
算法·leetcode·链表
简简单单做算法5 小时前
基于GA遗传优化双BP神经网络的时间序列预测算法matlab仿真
神经网络·算法·matlab·时间序列预测·双bp神经网络
阿豪学编程6 小时前
面试题map/unordered相关
数据结构
guygg886 小时前
利用遗传算法解决列车优化运行问题的MATLAB实现
开发语言·算法·matlab
武藤一雄6 小时前
19个核心算法(C#版)
数据结构·windows·算法·c#·排序算法·.net·.netcore
sali-tec6 小时前
C# 基于OpenCv的视觉工作流-章52-交点查找
图像处理·人工智能·opencv·算法·计算机视觉
梦想的颜色6 小时前
mongoTemplate + Java 增删改查基础介绍
数据结构·数据库·mysql