句子读单词

在一行中输入一个英文句子(不超过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--;
    }
    
}
相关推荐
Aldrich_321 小时前
蓝桥杯嵌入式赛道—-软件篇(GPIO输出模式配置)
c语言·vscode·stm32·单片机·嵌入式硬件·蓝桥杯
Kisorge2 小时前
【电机控制】基于STM32F103C8T6的二轮平衡车设计——LQR线性二次线控制器(算法篇)
stm32·嵌入式硬件·算法
@卞2 小时前
C语言常见概念
c语言·开发语言
hnjzsyjyj2 小时前
洛谷 P12141:[蓝桥杯 2025 省 A] 红黑树
数据结构·蓝桥杯·二叉树
铭哥的编程日记3 小时前
深入浅出蓝桥杯:算法基础概念与实战应用(二)基础算法(下)
算法·职场和发展·蓝桥杯
Swift社区3 小时前
LeetCode 421 - 数组中两个数的最大异或值
算法·leetcode·职场和发展
cici158743 小时前
基于高光谱成像和偏最小二乘法(PLS)的苹果糖度检测MATLAB实现
算法·matlab·最小二乘法
fei_sun3 小时前
【总结】数据结构---排序
数据结构
StarPrayers.4 小时前
自蒸馏学习方法
人工智能·算法·学习方法
芝麻馅汤圆儿4 小时前
c文件编译
c语言·开发语言