Solving Programming Challenges: C Programming for Word Frequency Analysis

Solving Programming Challenges: C Programming for Word Frequency Analysis

Recently, I came across a fascinating question and was impressed by Jeff Leach's response, particularly because he used C, a language that many programmers might not consider as their primary choice for string handling programs.

The problem statement was quite intriguing. The mixed use of language and the vague term 'average' required a good understanding of English to fully grasp. However, Jeff's solution almost gave the answer right away:

Question: Write a Program to Calculate Average Weight

The problem statement is as follows:

'Write that a program that: prompts the user to enter the exact size of a team the minimum is three, inputs the weight of each team member, and prints the average weight of the team.'

The solution involves a straightforward algorithm:

Declare mean and total weight as real numbers and set them to zero. Ask the user for the size of a team and store it. Loop through the size of the team and input the weight of each team member, adding it to the total weight. Calculate the mean as total weight divided by the size of the team. Print the answer.

This approach is simple and effective, making it easier for others to understand and implement.

Implementing a C Program to Analyze Word Frequency

While the first problem was straightforward, the second part of the problem presented a more complex challenge. The task was to write a program that accepts a sentence, displays the word or words that appear the most frequently, and provides an example solution in C:

Problem Statement

'Write a program that accepts a sentence a collection of words as input and displays the word or words that appears the most.'

Example:

Input:

Please enter the sentence: when I leave home I miss home

Output:

Output: brAppearances: 2

Code Implementation

Here's how I implemented this in C:

#include stdio.h
#include stdlib.h
#include string.h
#include ctype.h
#define MAX_CHARS_IN_SENTENCE 100
#define MAX_WORDS_IN_SENTENCE 100
#define MAX_CHARS_IN_WORD 20
char achWordsInSentence[MAX_WORDS_IN_SENTENCE 1][MAX_CHARS_IN_WORD 1];
int flagWordFound;
char achFoundWords[MAX_WORDS_IN_SENTENCE 1][MAX_chars_IN_WORD 1];
int anFoundWordsCount[MAX_WORDS_IN_SENTENCE 1];
int nFoundWordsCountMaximum, nCharCount, nWordCount, nSentenceLength, nFoundWordsTotal;
typedef enum { false, true } bool;
int main() {
  char ch;
  int i, j;
  char achReadSentence[MAX_CHARS_IN_SENTENCE 1];
  while (1) {
    printf(Enter a sentence (type 'done' to exit): );
    fgets(achReadSentence, MAX_CHARS_IN_SENTENCE, stdin);
    achReadSentence[strcspn(achReadSentence, 
)]  0;
    if (strcmp(achReadSentence, done)  0 || strcmp(achReadSentence, DONE)  0 || strcmp(achReadSentence, dONe)  0) {
      break;
    }
    nCharCount  0;
    nWordCount  0;
    for (i  0; i  MAX_WORDS_IN_SENTENCE; i  ) {
      for (j  0; j  MAX_CHARS_IN_WORD; j  ) {
        achWordsInSentence[i][j]  0;
      }
    }
    nSentenceLength  strlen(achReadSentence);
    for (i  0; i  nSentenceLength; i  ) {
      ch  achReadSentence[i];
      if (isspace(ch)) {
        nCharCount  0;
        continue;
      }
      if (nCharCount  0) {
        nWordCount  ;
      }
      achWordsInSentence[nWordCount][nCharCount]  ch;
      nCharCount  ;
    }
    nFoundWordsTotal  0;
    for (j  0; j  MAX_WORDS_IN_SENTENCE; j  ) {
      anFoundWordsCount[j]  0;
    }
    for (i  0; i  nWordCount; i  ) {
      flagWordFound  false;
      for (j  0; j  nFoundWordsTotal; j  ) {
        if (strcmp(achFoundWords[j], achWordsInSentence[i])  0) {
          anFoundWordsCount[j]  ;
          flagWordFound  true;
          break;
        }
      }
      if (!flagWordFound) {
        nFoundWordsTotal  ;
        strcpy(achFoundWords[nFoundWordsTotal], achWordsInSentence[i]);
        anFoundWordsCount[nFoundWordsTotal]  1;
      }
    }
    nFoundWordsCountMaximum  0;
    for (j  0; j  nFoundWordsTotal; j  ) {
      if (anFoundWordsCount[j] ! 0) {
        if (anFoundWordsCount[j]  nFoundWordsCountMaximum) {
          nFoundWordsCountMaximum  anFoundWordsCount[j];
        }
      }
    }
    printf(The word(s) with the highest frequency: );
    for (j  0; j  nFoundWordsTotal; j  ) {
      if (anFoundWordsCount[j]  nFoundWordsCountMaximum) {
        printf( achFoundWords[j]);
      }
    }
    printf( Other count: nFoundWordsCountMaximum 
);
  }
  return 0;
}

This program reads a sentence from the user, processes the words, and prints the most frequent words along with their counts. It uses loops, arrays, and string comparison functions from the standard library.

Conclusion

C is a powerful language that offers a robust set of tools for handling strings and other data, even when the problem statement and language are mixed and difficult to follow. By breaking down the problem into smaller steps and using well-documented code, you can solve seemingly complex challenges with relative ease.

Engage with these types of challenges to improve your C programming skills and gain a deeper understanding of the language. Happy coding!