Skip to content
C project #15Intermediate

Text Analyzer

Word, line, sentence and character statistics plus the most frequent words, using a hand-written hash table.

Estimated time
~5 hours to build
Files
6 files · 287 lines
Technologies
C11, Hash tables, Command-line arguments

Introduction

Counting words is easy; counting every distinct word in a large file efficiently needs a proper data structure. This project implements a hash table with separate chaining and uses it to produce a frequency report.

Problem statement

Read a text file named on the command line (default sample.txt) and print the number of lines, words, characters and sentences, the number of distinct words, the average word length, the longest word, and the top N most frequent words with a bar chart.

Objectives

  • Implement a hash table with chaining and the djb2 hash
  • Process a file character by character
  • Sort an array of pointers with qsort
  • Free every allocation

Features

  • Case-insensitive word counting (apostrophes kept)
  • Line, sentence and character totals
  • Longest word and average length
  • Top-N frequency report with bars
  • File name and N from the command line

Concepts demonstrated

  • Hashing
  • Linked lists
  • Pointer arrays
  • Character classification

System architecture

A 4093-bucket array of linked lists stores (word, count) entries; add_word hashes the word, walks the chain and either increments or prepends. After reading, all entries are collected into an array of pointers and sorted by count (then alphabetically) for the report.

Folder structure

TextProject tree
text-analyzer/
├── README.md
├── Makefile
├── sample.txt
├── src/main.c
├── tests/args.txt
└── tests/expected-output.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra -O2
SRC := $(wildcard src/*.c)
HDR := $(wildcard src/*.h)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CC) $(CFLAGS) -Isrc $(SRC) -o app -lm
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/main.c
The hash table (hash, add_word, free_table), the character-level scanner and the report.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
gcc -std=c11 -Wall -Wextra -Isrc src/*.c -o app -lm
# or simply: make

Run

Terminal
./app sample.txt 6 # Linux / macOS
.\app.exe sample.txt 6 # Windows

Test against the expected output

Terminal
./app sample.txt 6 > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# .\app.exe sample.txt 6 > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
File: sample.txt
Lines: 10 Words: 99 Characters: 549 Sentences: 8
Distinct words: 63 Average word length: 4.34
Longest word: programming (11 letters)
Top 6 words:
1. a 5 *****
2. c 5 *****
3. is 5 *****
4. the 5 *****
5. you 5 *****
6. an 3 ***

Testing

  • ./app sample.txt 6 must match tests/expected-output.txt.
  • Run it on a large file (a book from Project Gutenberg) and time it.
  • Run on an empty file: it must report 0 words without crashing.

Common errors

"don't" counted as "don" and "t"

Apostrophes inside a word are kept; check the c == '\'' condition in the scanner.

Crash on very long "words"

Characters beyond MAX_WORD − 1 are ignored, protecting the buffer.

Possible improvements

  • Ignore common stop words (the, a, is)
  • Report word lengths as a histogram
  • Process several files and compare them

Additional challenges

  1. 1Grow the hash table when the load factor exceeds 0.75.
  2. 2Find the most common two-word phrases (bigrams).
  3. 3Compute a readability score (Flesch reading ease).

Built and tested it yourself? Mark it to update your dashboard.