Lesson 6 of 7
Mini Project Part 4 - Word Frequency Counter
Build a word frequency counter that reads a text string, tokenises it using `strtok`, converts each word to lowercase, and counts how many times each unique word appears. Applies: char arrays, string functions (strtok, strlen, strcasecmp/tolower, strcmp), 2D char arrays for the word table, and careful buffer handling.
Word Frequency Counter - Complete Implementation
Word Frequency Counter - Complete Implementation
/* wordfreq.c - count word frequencies in a text string */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORDS 128
#define MAX_WORD_LEN 64
/* Convert string to lowercase in-place */
static void to_lower(char *s) {
for (; *s; s++) *s = (char)tolower((unsigned char)*s);
}
/* Strip leading/trailing punctuation from a word */
static void strip_punct(char *word) {
/* Remove trailing punctuation */
int len = (int)strlen(word);
while (len > 0 && !isalnum((unsigned char)word[len-1])) {
word[--len] = '