每日一题——第一百一十一题

题目:统计在字符串中出现的单词的个数

#include<stdio.h>
#include<stdbool.h>
#include<string.h>

int FindWordInStr(char* arr) {
	bool inWord = false, lastInWord = false;
	int count = 0, len = strlen(arr);
	for (int i = 0; i < len; i++) {

		if (arr[i] != ' ') {
			inWord = true;
			lastInWord = true;
			
		}
		else { //当前字符为空字符
			//首先判断之前是否在一个单词中,如果在,让单词数+1
			if (lastInWord) {
				count++;
				lastInWord = false;//将状态复位
			}
			else {//之前不在一个单词中
				inWord = false;//不在单词状态置为true
			}
			
		}
		
	}

	// 检查字符串末尾是否在一个单词中(检查最后一个字符是否为非空字符)  
	if (lastInWord && arr[len - 1] != ' ') {
		count++;
	}

	return count;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值