A powerful command-line tool written in C that analyzes text files and generates detailed reports on character frequency, word frequency, and basic file statistics.
- Total character, word, and line count
- Character frequency analysis (all printable characters)
- Word frequency analysis (case-insensitive, punctuation-aware)
- Flexible command-line options to control report output
- Save reports to a file with the
-oflag - Modular codebase with clean separation of concerns
- Zero memory leaks (verified with Valgrind)
text-file-analyzer/
├── Makefile # Build system
├── main.c # Entry point, argument parsing, report generation
├── analyzer.h # Public interface for the analysis engine
├── analyzer.c # Core file reading and analysis logic
├── hashtable.h # Public interface for the hash table module
├── hashtable.c # Hash table implementation (separate chaining)
├── test.txt # Sample test file
└── edge_cases.txt # Edge case test file
- GCC compiler
- Make
- macOS or Linux
Clone the repository and build the project:
git clone https://github.com/samarth-swami/text-file-analyzer.git
cd text-file-analyzer
makeThis will compile all source files and generate the analyzer executable.
./analyzer [options] <filename>| Option | Description |
|---|---|
-c, -w, -l |
Show overall statistics (characters, words, lines) |
--freq |
Show character and word frequency tables |
-o <file> |
Write the report to a file instead of the console |
If no options are specified, the full report is shown by default.
Run a full report:
./analyzer test.txtShow only overall statistics:
./analyzer -c test.txtShow only frequency tables:
./analyzer --freq test.txtSave report to a file:
./analyzer -o report.txt test.txtCombine options — stats only, saved to file:
./analyzer -c -o report.txt test.txt--- Analysis Report for test.txt ---
Overall Statistics:
Total Characters: 51
Total Words: 11
Total Lines: 3
Character Frequency:
Character Count
--------- -----
H 1
e 6
l 5
...
Word Frequency:
Word Count
-------------------- -----
hello 1
world 1
test 1
...
- Argument Parsing —
main.cparses command-line flags and options. - Analysis Engine —
analyzer.creads the file character by character, counting characters, words, and lines, and building words into a buffer. - Hash Table —
hashtable.cimplements a hash table with separate chaining to store and count unique words efficiently using the djb2 hash algorithm. - Report Generation — Results are printed to the console or written to a file based on user options.
To remove compiled files:
make cleanTo do a full rebuild:
make re- djb2 Hash Function for fast and uniform word distribution
- Separate Chaining for collision resolution in the hash table
- Dynamic Memory Management with full cleanup on all exit paths
- Modular Architecture separating analysis, data structures, and reporting
- Case-insensitive word frequency counting using
tolower() - Buffer overflow protection with
MAX_WORD_LENlimit
This project is open source and available under the MIT License.