Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: Program tha...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 7 of 16 Topic 26192 of 26972
Post > Topic >>

Re: Program that makes a list of words.

by Richard Heathfield <rjh@[EMAIL PROTECTED] > May 13, 2008 at 08:52 AM

lundslaktare@[EMAIL PROTECTED]
 said:

<snip>

> The thing is, that I don't know enough of C (or any other language for
> that matter)
> that I can write such a program myself.

Well, perhaps you can learn.

> I asked my Father, and he suggested that I should use microsoft-word
> to make the whole text one column,
> and then insert that column in excel and use the sorting
> function.

Blech!  :-)

> But that wont work for long text(more than 65536 words).

And it's so inelegant, too.

> I think it's just natural to count
> mor-
> ning
> 
> as morning and -

I think it's more natural to think of it as one word that has been split 
across two lines, and the hyphen is furniture that can be discarded.

> At least you don't want to miss a word just because it's written on
> two lines.
> 
> Do you know a simple program that don't use megs of data?

The amount of data is up to you, since that's decided at runtime.

The best book on C programming is "The C Programming Language", 2nd 
edition, by Kernighan and Ritchie. Exercise 6-4 of that book is: "Write a 
program that prints the distinct words in its input sorted into decreasing

order of frequency of occurrence. Precede each word by the count."

Bryan Williams has written a solution to this exercise, available here:

http://clc-wiki.net/wiki/K%26R2_solutions%3AChapter_6%3AExercise_4

We can hack this to produce a list sorted by word rather than by count, by

writing this function:

int CompareWords(const void *vWord1,
                 const void *vWord2)
{
  WORD *const *Word1 = vWord1;
  WORD *const *Word2 = vWord2;

  assert(NULL != vWord1);
  assert(NULL != vWord2);
  return strcmp((*Word1)->Word, (*Word2)->Word);
}

....and by adding a prototype for it:

int CompareWords(const void *vWord1,
                 const void *vWord2);

just under the similar prototype for CompareCounts, and by replacing the 
qsort call with this line:

    qsort(WordArray, Treecount, sizeof *WordArray, CompareWords);

We can fix the output to the way you want it by changing:

    fprintf(Dest, "%10lu %s\n", (unsigned long)WordArray[Pos]->Count,
            WordArray[Pos]->Word);

to this:

    fprintf(Dest,
            "%-30s\t%10lu\n",
            WordArray[Pos]->Word,
            (unsigned long)WordArray[Pos]->Count);


After making these changes and re-compiling (see 
http://www.cpax.org.uk/prg/****table/c/resources.php
for a list of free C 
compilers), running the program (and using its own source code as input) 
results in output that starts like this:

AddToTree                                5
Assumptions                              1
Author                                   1
Bryan                                    1
Buf                                      4
CANNOT_MALLOC_WORDARRAY                  2
Chapter                                  1
CompResult                               4
CompareCounts                            3

(and goes on for another 219 lines).

This is very nearly what you want, but not quite, because it doesn't
handle 
your "join halfwords that are split by a hyphen" requirement. If you want 
that, you'll either have to find some other kind soul who has more spare 
time than I do, or learn enough about C to make that change yourself.

-- 
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www.
+rjh@[EMAIL PROTECTED]
 users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 




 16 Posts in Topic:
Program that makes a list of words.
lundslaktare@[EMAIL PROTE  2008-05-12 23:32:01 
Re: Program that makes a list of words.
Chris McDonald <chris@  2008-05-13 06:43:04 
Re: Program that makes a list of words.
lundslaktare@[EMAIL PROTE  2008-05-13 00:12:08 
Re: Program that makes a list of words.
Chris McDonald <chris@  2008-05-13 07:24:44 
Re: Program that makes a list of words.
Richard Heathfield <rj  2008-05-13 07:38:04 
Re: Program that makes a list of words.
lundslaktare@[EMAIL PROTE  2008-05-13 01:22:26 
Re: Program that makes a list of words.
Richard Heathfield <rj  2008-05-13 08:52:13 
Re: Program that makes a list of words.
viza <tom.viza@[EMAIL   2008-05-13 04:48:53 
Re: Program that makes a list of words.
"Joachim Schmitz&quo  2008-05-13 13:55:22 
Re: Program that makes a list of words.
pete <pfiland@[EMAIL P  2008-05-13 07:59:33 
Re: Program that makes a list of words.
Richard Heathfield <rj  2008-05-13 12:22:16 
Re: Program that makes a list of words.
Keith Thompson <kst-u@  2008-05-13 11:14:39 
Re: Program that makes a list of words.
CBFalconer <cbfalconer  2008-05-13 10:21:13 
Re: Program that makes a list of words.
viza <tom.viza@[EMAIL   2008-05-13 11:30:02 
Re: Program that makes a list of words.
viza <tom.viza@[EMAIL   2008-05-13 11:37:06 
Re: Program that makes a list of words.
CBFalconer <cbfalconer  2008-05-13 16:28:27 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 21:42:47 CDT 2008.