14781 - Counting Word Frequency   

Description

(30 points)

Write a program that counts how many times each English word appears in the input text.

A word is defined as a sequence of consecutive English letters (A–Z or a–z).
Letters are case-sensitive, meaning "This" and "this" are considered different words.

Your program must read all input until end-of-file (EOF) and count the frequency of every distinct word found.

The output should be sorted by Frequency in ascending order; however, if two or more words have the same frequency, they should be sorted by the order defibned by the strcmp function in the C standard library (in <string.h>).

 

About strcmp function.

The strcmp() function compares two strings (str1 and str2) according to their ASCII.
It checks each character of both strings from left to right until:

  • The characters differ, or

  • The null terminator ('\0') is reached

Return Value Meaning Example
< 0 str1 comes before str2 in ASCII  strcmp("apple", "banana") → negative value
 = 0 str1 and str2 are identical strcmp("cat", "cat") → 0
 > 0 str1 comes after str2 in ASCII  strcmp("dog", "cat") → positive value

 

Example: String Sorting Using strcmp()

#include <stdio.h>
#include <string.h>

#define N 80
#define M 100

int main(void) {
    char s[N][M];       // input string
    char *ptrs[N];      // pointer array pointing to input sting
    char *temp;         // temp string for sorting
    int count = 0;      // number of input string
    int i, j;           // loop lindices


    // Read input string and store in to array s
    while (scanf("%s", s[count]) != EOF && count < N) {
        ptrs[count] = s[count];         // point to the current input string
        count++;                        // accumulate the number of input string
    }


    //sort the input string in ASCII 
    // only sort the pointer array poining to the input string
    for (i = 0; i < count - 1; i++) {
        for (j = i; j < count; j++) {
            if (strcmp(ptrs[i], ptrs[j]) > 0) {
                temp = ptrs[i];
                ptrs[i] = ptrs[j];
                ptrs[j] = temp;
             }
         }
     }

    

    // print out the sorting result
    for (i = 0; i < count; i++) printf("%s\n", ptrs[i]);
    return 0;
}
Input of Example Code Output of Example Code
mountain
coffee
river
sunshine
Mirror
forest
planet
window
teacher
ocean
windows

Mirror
coffee
forest
mountain
ocean
planet
river
sunshine
teacher
window
windows

Hint

You may need to sort strings in this program. You can modify the example code directly to complete your solution.

Input

  • The input consists of one or more lines of English text.

  • Input continues until EOF is reached.

  • You may assume:

    • The length of each line n satisfies 0 < n ≤ 80

    • The total number of distinct words m satisfies 0 < m ≤ 100

The commas and periods have been removed from the test data, so you don’t need to consider them.

Output

  • Output all distinct words and their corresponding frequencies.

  • Sort the output by:

    1. Increasing order of frequency

    2. ASCII (A–Z, a-z) when frequencies are equal

Each line of the output should contain a word’s frequency, followed by a space, and then the word itself.

Ensure that the output, including formatting 輸出格式, exactly matches the provided samples.

Sample Input  Download

Sample Output  Download

Tags




Discuss