Build a word counter · Coding Class
This topic is not available in the selected language or version. Showing the start of this section.
Apply the chapter in three steps: an exercise, a program project, and a debugging task. Implement first, then consult the hints and completion criteria. Use a separate file or working directory for each task.
1. Exercise · Check word boundaries
Section titled “1. Exercise · Check word boundaries”Create an empty file, a file containing only spaces/tabs/newlines, and a file containing one two three four . Run the chapter’s word counter on each.
Completion: The counts are 0, 0, and 4. Count words rather than lines.
Hint: Count items after splitting the whole file on ASCII whitespace.
2. Build a program · Build a multi-file report
Section titled “2. Build a program · Build a multi-file report”Accept several paths and print path: count per file. Prepare a.txt with one two and an empty b.txt. Report read failures as path: error and continue.
Completion: For a.txt, absent missing.txt, b.txt in that order, output is a.txt: 2, missing.txt: error, b.txt: 0.
Hint: Replace the single-argument check with an index loop and match each count_file result.
3. Debug · Fix argument forwarding
Section titled “3. Debug · Fix argument forwarding”Using the chapter’s single-file version, diagnose and fix xen run word_count.xen input.txt and ./word-count -- input.txt.
Completion: Correct commands are xen run word_count.xen -- input.txt and ./word-count input.txt; both print the same count.
Hint: Distinguish the Xen CLI separator from the program’s own arguments.
Verify and record
Section titled “Verify and record”Use xen check file.xen followed by xen run file.xen for ordinary programs; use xen test file.xen for test-only files. Record the original error, its cause, and the corrected result, then rerun every input in the completion criteria.