句子读单词

在一行中输入一个英文句子(不超过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--;
    }
    
}
相关推荐
mmmmath_31 小时前
LeetCode.541.反转字符串II
数据结构·算法·leetcode
Navigator_Z1 小时前
LeetCode //MySQL - 1251. Average Selling Price
c语言·算法·leetcode
醇氧2 小时前
MySQL 8.0 系统表损坏与引擎转换故障排查实战
数据结构·算法
Escalating_xu2 小时前
【C语言数据类型和变量·上】从内置类型到变量模型:一次讲透 sizeof、signed/unsigned 与作用域
c语言
大熊背2 小时前
《Color constancy by characterization of illumination chromaticity》之色度色域最大化算法(二)
算法·白平衡·色度·色温
钓鱼的肝3 小时前
梳理(1-5)
c++·经验分享·笔记·算法·青少年编程
aramae3 小时前
模拟实现strlen()函数 (C语言)
c语言·开发语言·后端
HZZD_HZZD3 小时前
CSDN_批发市场水电漏损归因算法LAM的原理与落地
嵌入式硬件·物联网·算法
shirsl5 小时前
算法 Day 5 树 / 二叉树 + DFS
数据结构·python·算法