Help: for loop doesn't work?

Discussion in 'C Programming' started by a, Dec 21, 2007.

1. aGuest

I'm studying the frequency of 5-letter words composed of {a,b,c,d,e} in a
string, I wanna build an array recording the frequencies of (aaaaa, aaaab,
...., caaaa, ..., eeeee). I find that for loop can work well if I hard-code
the number, what can I do if I wanna study n-letter word (e.g. n=3, 100,
3732)?

a, Dec 21, 2007

a wrote:
> I'm studying the frequency of 5-letter words composed of {a,b,c,d,e} in a
> string, I wanna build an array recording the frequencies of (aaaaa, aaaab,
> ..., caaaa, ..., eeeee). I find that for loop can work well if I hard-code
> the number, what can I do if I wanna study n-letter word (e.g. n=3, 100,
> 3732)?

I suggest, for a given n, making a single pass through the string, building
a data structure containing all the defined words and a count of how many
times each is used. A tree or hash table would work well.

--

Thad Smith, Dec 25, 2007

3. CBFalconerGuest

> a wrote:
>
>> I'm studying the frequency of 5-letter words composed of
>> {a,b,c,d,e} in a string, I wanna build an array recording the
>> frequencies of (aaaaa, aaaab, ..., caaaa, ..., eeeee). I find
>> that for loop can work well if I hard-code the number, what can
>> I do if I wanna study n-letter word (e.g. n=3, 100, 3732)?

>
> I suggest, for a given n, making a single pass through the string,
> building a data structure containing all the defined words and a
> count of how many times each is used. A tree or hash table would
> work well.

Take a look at the demonstration application for hashlib, wdfreq.
Modification should do what the OP wants. hashlib is available,

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

CBFalconer, Dec 25, 2007