C语言:单词统计

题目描述

给定一个英文句子,单词之间用空格分隔,统计有多少个单词,输出统计结果。

输入输出格式

输入格式

一行,英文句子
输出格式

一个整数,句子中单词的个数

复制代码
#include <stdio.h>
#include <math.h>
#include <string.h>

int main()
{
	char str[1000];
	gets(str);
	
	int i;
	int inword=0,count=0;
	for(i=0;str[i]!='\0';i++)
	{
		if(str[i]==' '&&inword==1)
		{
			count++;
			inword=0;
		}else if(str[i]!=' ')
		{
			inword=1;
		}
	}
	
	if(inword==1)
	{
		count++;
	}
	
	printf("%d",count);
	return 0;
}
相关推荐
程序猿_极客1 小时前
【2025 年最新版】Java JDK 安装与环境配置教程(附图文超详细,Windows+macOS 通用)
java·开发语言·windows·macos·jdk
二哈喇子!4 小时前
BOM模型
开发语言·前端·javascript·bom
C++ 老炮儿的技术栈4 小时前
在C++ 程序中调用被 C编译器编译后的函数,为什么要加 extern “C”声明?
c语言·c++·windows·git·vscode·visual studio
二哈喇子!4 小时前
空指针异常
开发语言
咚为5 小时前
Rust Print 终极指南:从底层原理到全场景实战
开发语言·后端·rust
%xiao Q5 小时前
GESP C++五级-202406
android·开发语言·c++
Psycho_MrZhang5 小时前
Neo4j Python SDK手册
开发语言·python·neo4j
Traced back5 小时前
# C# + SQL Server 实现自动清理功能的完整方案:按数量与按日期双模式
开发语言·c#
sin22015 小时前
MyBatis的执行流程
java·开发语言·mybatis
web3.08889995 小时前
1688图片搜索API,相似商品精准推荐
开发语言·python