Statistics on Word Frequency in C language and Word Frequency in C Language

Source: Internet
Author: User

Statistics on Word Frequency in C language and Word Frequency in C Language

Requirements:

1. Design a word frequency statistics software to count the word frequency of a given English article.

2. punctuation included in the article is not included in statistics.

3. output the statistical results in ascending order.

 

Design:

1. Because it is a cross-disciplinary 0.0 · and does not use c ++ or java, it is quite difficult to compile it with only the learned c language.

2. Define a struct containing the word and frequency members to calculate the word frequency (the memory is dynamically allocated to process large text ).

3. Use the fopen function to read the specified document.

4. Use the fgetc function to obtain characters, and then perform different processing based on whether the obtained characters are letters or not.

5. Sort the statistical results by quick sorting.

5. output the entire statistical result cyclically.

 

Some code:

 

Struct definition:

struct fre_word{    int num;    char a[18];}; 

Allocate initial memory:

Struct fre_word * w; w = (struct fre_word *) malloc (100 * p * sizeof (struct fre_word); // allocate the initial memory to the struct

 

Read text:

Printf ("enter the name of the file to be read:"); scanf ("% s", filename); // enter the name of the file whose word frequency needs to be counted if (fp = fopen (filename, "r") = NULL) {printf ("file cannot be opened \ n"); exit (0 );}

 

Word matching:

/*************** Set the number of occurrences of a word to 1 **************** * ***********/for (I = 0; I <100; I ++) {(w + I)-> num = 1 ;} ********************* * ******************/I = 0; while (! Feof (fp) // The file has not been read. {ch = fgetc (fp); (w + I)-> a [j] = '\ 0 '; if (ch> = 65 & ch <= 90 | ch> = 97 & ch <= 122) // if ch is a letter, it is saved to {(w + I) -> a [j] = ch; j ++; flag = 0; // set the flag to determine whether consecutive punctuation marks or spaces exist.} else if (! (Ch> = 65 & ch <= 90 | ch> = 97 & ch <= 122) & flag = 0) // If ch is not a letter and the last character is the letter {I ++; j = 0; flag = 1; for (m = 0; m <I-1; m ++) // match the word. if the word already exists, num + 1 {if (stricmp (w + m)-> a, (w + i-1)-> a) = 0) {(w + m)-> num ++; I --;}}} ******************** * ******************/if (I = (p * 100 )) // use I to determine if the current memory is full {p ++; w = (struct fre_word *) realloc (w, 100 * p * (sizeof (struct fre_word ))); for (n = I; n <= 100 * p; n ++) // assign the initial value (w + n) to the newly allocated memory struct-> num = 1 ;}}

 

Quick sorting:

Void quick (struct fre_word * f, int I, int j) {int m, n, temp, k; char B [18]; m = I; n = j; k = f [(I + j)/2]. num; // select do {while (f [m]. num> k & m <j) m ++; // find elements smaller than k from left to right while (f [n]. num <k & n> I) n --; // from the right to the left, find an element greater than k if (m <= n) {// if it is found and meets the conditions, then switch temp = f [m]. num; strcpy (B, f [m]. a); f [m]. num = f [n]. num; strcpy (f [m]. a, f [n]. a); f [n]. num = temp; strcpy (f [n]. a, B); m ++; n -- ;}} while (m <= n); if (m <j) quick (f, m, j ); // use recursive if (n> I) quick (f, I, n );}

 

Result output:

For (n = 0; n <= I; n ++) {printf ("the word in the document:"); printf ("%-18 s ", (w + n)-> a); printf ("the number of occurrences is:"); printf ("% d \ n", (w + n) -> num );}

 

Test cases:

After reading the comments from previous blogs and teachers, I used a long text for the test, using Obama's inaugural speech.

 

Some test results:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.