C语言实例1—统计单词个数

我使用的DEVC++编译器,新建get_words_num.c文件

c 复制代码
#include<stdio.h>

int main()
{
	char cString[100];
	
	int iIndex,iWord=1;
	char cBlank;
	gets(cString);
	
	if(cString[0]=='\0')
	{
		printf("There is no words!\n");
	}
	else if(cString[0]==' ')
	{
		printf("first words is a blank char!\n");
	}
	else
	{
		for (iIndex=0;cString[iIndex]!='\0';iIndex++)
		{
			cBlank=cString[iIndex];
			if(cBlank==' '){
				iWord++;
			}
			
		}
		printf("%d\n",iWord);
	}
	return 0 ;
}

运行

c 复制代码
I love cats and dags
5
c 复制代码
 I love cats and dags
 first words is a blank char!

直接回车

c 复制代码
There is no words!
相关推荐
Python私教1 天前
Python 开发环境安装与配置全指南(2025版)
开发语言·python
百锦再1 天前
第12章 测试编写
android·java·开发语言·python·rust·go·erlang
无敌最俊朗@1 天前
C++ 并发与同步速查笔记(整理版)
开发语言·c++·算法
Elastic 中国社区官方博客1 天前
Observability:适用于 PHP 的 OpenTelemetry:EDOT PHP 加入 OpenTelemetry 项目
大数据·开发语言·人工智能·elasticsearch·搜索引擎·全文检索·php
csbysj20201 天前
PHP 魔术常量
开发语言
奔跑吧邓邓子1 天前
【C语言实战(80)】C语言实战:从复盘到进阶,解锁编程新高度
c语言·进阶·复盘·终篇
狮子不白1 天前
C#WEB 防重复提交控制
开发语言·前端·程序人生·c#
@曾记否1 天前
如何在Keil5中在没有硬件支持的情况下使用Keil的模拟器(Simulator) + 调试窗口输出进行调试
c语言·stm32
烛衔溟1 天前
C语言多级指针与函数指针:指针的高级用法
c语言·算法