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!
相关推荐
Sylvia-girl2 小时前
Java——抽象类
java·开发语言
Yana.nice4 小时前
Bash函数详解
开发语言·chrome·bash
tomorrow.hello6 小时前
Java并发测试工具
java·开发语言·测试工具
晓13136 小时前
JavaScript加强篇——第四章 日期对象与DOM节点(基础)
开发语言·前端·javascript
老胖闲聊6 小时前
Python I/O 库【输入输出】全面详解
开发语言·python
她说人狗殊途7 小时前
java.net.InetAddress
java·开发语言
天使day7 小时前
Cursor的使用
java·开发语言·ai
Dxy12393102168 小时前
Python ExcelWriter详解:从基础到高级的完整指南
开发语言·python
源代码•宸9 小时前
C++高频知识点(十三)
开发语言·c++·经验分享·面经
wa的一声哭了10 小时前
python基础知识pip配置pip.conf文件
java·服务器·开发语言·python·pip·risc-v·os