guuzaa/zig-wc
A simple word count program written in Zig.
A modern implementation of the classic Unix wc
utility written in the Zig programming language. This tool counts lines, words, and characters in text files or from standard input.
Clone the repository and build using Zig's build system:
git clone https://github.com/yourusername/wc.git
cd wc
zig build
The executable will be available at zig-out/bin/wc
.
zig build install
Usage: wc [OPTION]... [FILE]...
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified. If no FILE is specified, read standard input.
Options:
-c print the byte counts
-l print the newline counts
-w print the word counts
-h, --help display this help and exit
With no options, print line, word, and byte counts.
Count lines, words, and characters in a file:
wc file.txt
Count only lines in multiple files:
wc -l file1.txt file2.txt
Count words from standard input:
cat file.txt | wc -w
src/main.zig
: Entry point and main program flowsrc/counter.zig
: Core counting functionalitysrc/options.zig
: Command-line options handlingsrc/formatter.zig
: Output formattingsrc/cli.zig
: Command-line argument parsingbuild.zig
: Build configurationzig build test
# Debug build
zig build -Doptimize=Debug
# Release build
zig build -Doptimize=ReleaseFast
The program reads input either from files specified on the command line or from standard input if no files are provided. It counts:
\n
)For each file, it displays the requested counts along with the filename. If multiple files are processed, a total line is displayed at the end.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the Unlicense License - see the LICENSE.txt
file for details.