Skip to content

Sam-X-Dev/text-file-analyzer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Text File Analyzer

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.

Features

  • 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 -o flag
  • Modular codebase with clean separation of concerns
  • Zero memory leaks (verified with Valgrind)

Project Structure

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

Requirements

  • GCC compiler
  • Make
  • macOS or Linux

Installation

Clone the repository and build the project:

git clone https://github.com/samarth-swami/text-file-analyzer.git
cd text-file-analyzer
make

This will compile all source files and generate the analyzer executable.

Usage

./analyzer [options] <filename>

Options

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.

Examples

Run a full report:

./analyzer test.txt

Show only overall statistics:

./analyzer -c test.txt

Show only frequency tables:

./analyzer --freq test.txt

Save report to a file:

./analyzer -o report.txt test.txt

Combine options — stats only, saved to file:

./analyzer -c -o report.txt test.txt

Sample Output

--- 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
  ...

How It Works

  1. Argument Parsingmain.c parses command-line flags and options.
  2. Analysis Engineanalyzer.c reads the file character by character, counting characters, words, and lines, and building words into a buffer.
  3. Hash Tablehashtable.c implements a hash table with separate chaining to store and count unique words efficiently using the djb2 hash algorithm.
  4. Report Generation — Results are printed to the console or written to a file based on user options.

Cleaning Up

To remove compiled files:

make clean

To do a full rebuild:

make re

Technical Highlights

  • 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_LEN limit

License

This project is open source and available under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors