Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

Word Count

This program is the D version of the classic wc (wordcount) C program. It serves to demonstrate how to read files, slice arrays, and simple symbol table management with associative arrays.

import std.stdio;
import std.algorithm;

int main(string[] args)
{
    ulong wordCount;
    ulong lineCount;
    ulong charCount;

    int[string] dictionary;
    writeln("   lines   words   bytes file");

    foreach(arg; args[1 .. args.length])
    {
        ulong lWordCount;
        ulong lCharCount;
        ulong lLineCount;

        auto file = File(arg);
        foreach(line; file.byLine(KeepTerminator.yes))
        {
            lCharCount += line.length;
            foreach(char[] word; splitter(line))
            {
                lWordCount += 1;
                if(auto count = word in dictionary)
                    *count += 1;
                else
                    dictionary[word.idup] = 1;
            }

            lLineCount += 1;
        }

        writefln("%8s%8s%8s %s\n", lLineCount, lWordCount, lCharCount, arg);

        wordCount += lWordCount;
        lineCount += lLineCount;
        charCount += lCharCount;
    }

    if (args.length > 2)
    {
        writefln("--------------------------------------\n%8s%8s%8s total",
                        lineCount, wordCount, charCount);
    }

    writeln("--------------------------------------");

    foreach (word; sort(dictionary.keys))
    {
            writefln("%3s %s", dictionary[word], word);
    }
    return 0;
}