[COMP] Re: C program to count the number of words in a text file
- From: Shane McKeown <shane_mckeown@xxxxxxxxx>
- To: computers@xxxxxxxxxxxxxxxxx
- Date: Tue, 1 May 2001 00:58:08 -0700 (PDT)
Thanks guys
I know the linked list method is the best, thanks
for all the help anyway.
Shane
--- John Madden <weez@xxxxxxxxxxxxx> wrote:
>
> On Monday 30 April 2001 14:45, you wrote:
> > Hi guys
> >
> > I have a small problem. A friend needs help
> writing a
> >
> > C program which has to read a text file, and count
> > the number of occurences of each word in the file.
> > I know this can be done using Linked-Lists, but
> his
> > version needs to use arrays.
> >
> > The input file has the following:
> > TO BE OR NOT TO BE
>
> Hmm. This shouldn't be *too* difficult, using
> something of a state
> machine that uses a space (obviously) as a word
> delimiter. There's no
> need for a linked list, but doing this with arrays
> is sort of... funny. :)
>
> Use two arrays: one to store the words, another to
> store the count. The
> quick-n-dirty but inefficient way of doing this
> would be to have a
> function to search for the word in the array and
> return its index:
>
> int findword(char target[])
> {
> int i;
> for (i=0; i<total_word_count; i++) // the count is
> global
> if(strcmp(word_array[i], target)==0) // word array
> is global
> return index; //loc of word in the array
> return -1; // word not in the array
> }
>
> So if you call this and get a -1, you know you have
> to add the word to the
> array:
>
> void addword(char word[])
> {
> word_array[total_word_count++] = word;
> }
>
> That'll put all of the words into the array. Then
> put a 1 inside
> word_count]total_word_count], the other global
> array, to hold the count
> for this word.
>
> And if you don't get a -1 from findword(), you'll
> have to use the index
> returned to increment that location in word_count:
> word_count["returned
> int"]++;
>
> I think you can figure out the rest. :)
>
> Anyway, the much-faster way of doing this would be
> to sort the words
> before you add them to the array -- this program
> will be really slow with
> large (like more than a couple hundred words) files
> -- but that increases
> the complexity of addword() and findword() by quite
> a bit.
>
> John
>
>
>
> --
> # John Madden weez@xxxxxxxxxxxxx ICQ: 2EB9EA
> # FreeLists, Free mailing lists for all:
> http://www.freelists.org
> # UNIX Systems Engineer, Ivy Tech State College:
> http://www.ivy.tec.in.us
> # Linux, Apache, Perl and C: All the best things in
> life are free!
> ========================================
> Avenir Web's Computers Mailing List
>
> List Modes, Subscription, and General Info:
> Go to
>
http://www.freelists.org/cgi-bin/webpage?webpage_id=11
>
> List Archives:
> http://www.freelists.org/archives/computers
> Administrative Contact: weez@xxxxxxxxxxxxx
>
> Get computer help: http://avenir.dhs.org
> ========================================
__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/
========================================
Avenir Web's Computers Mailing List
List Modes, Subscription, and General Info:
Go to http://www.freelists.org/cgi-bin/webpage?webpage_id=11
List Archives: http://www.freelists.org/archives/computers
Administrative Contact: weez@xxxxxxxxxxxxx
Get computer help: http://avenir.dhs.org
========================================
- Follow-Ups:
- [COMP] Re: C program to count the number of words in a text file
- From: James Gosnell
Other related posts:
- » [COMP] C program to count the number of words in a text file
- » [COMP] Re: C program to count the number of words in a text file
- » [COMP] Re: C program to count the number of words in a text file
- » [COMP] Re: C program to count the number of words in a text file
- » [COMP] Re: C program to count the number of words in a text file
- [COMP] Re: C program to count the number of words in a text file
- From: James Gosnell