关于找字符串中每个单词出现次数

来源:百度知道 编辑:UC知道 时间:2024/06/14 05:24:11
当用户输入一段文字
(用gets())
如果找到每个单词出现的次数

用户输入
"there are a lot of people, a lot of people in there"

结果:
there 2
are 1
a 2
lot 2
of 2
people 2
in 1
我是初学者
你可以在讲清楚一些吗?
dim是功能是什么?

谢谢

给你一个思路:自定义一个函数Dass,目的是统计在母串muchuan中,子串zichuan的数目。

function Dass(muchuan,zichuan) '定义函数开始
dim mlen '定义母串长度变量mlen
dim elen '定义将母串中的指定子串去除后的新串长度变量elen
dim zlen '定义子串长度变量zlen
mlen = len(muchuan) '计算母串长度
elen = len(replace(muchuan,zichuan,"")) '计算新串长度
zlen = len(zichuan) '计算子串长度
Dass = (mlen-elen)/zlen '计算母串中指定子串的数量
end function '定义函数结束
事实上,"there are a lot of people, a lot of people in there"就是一个母串,而“there”、“are ”、“a”等每个单词就是一个子串,用上面的方法循环调用Dass就可以统计出它们出现的次数。
现在明白了吗?