K&R C Chapter 6 Exercise Solutions — Structures

Chapter 6: Structures introduces struct, typedef, and self-referential structures (linked lists, binary trees). The chapter builds a word-frequency counter using a binary tree, then extends it with a hash table. Exercise 6-3 adds a cross-reference listing; 6-6 implements a rudimentary #define processor using a hash table — the most complex exercise in this chapter.

These are worked solutions to all 6 exercises. Each solution compiles cleanly with gcc -ansi -Wall.

Book: The C Programming Language, 2nd Ed — Kernighan & Ritchie  |  All chapters index


Exercises

  1. Exercise 6-1. Our version of getword does not properly handle underscores, string constants, comments, or preprocessor control lines. Write a better version.

    Solution to Exercise 6-1

  2. Exercise 6-2. Write a program that reads a C program and prints in alphabetical order each group of variable names that are identical in the first 6 characters, but different somewhere thereafter. Don’t count words within strings and comments. Make 6 a parameter that can be set from the command line.

    Solution to Exercise 6-2

  3. Exercise 6-3. Write a cross-referencer that prints a list of all words in a document, and for each word, a list of the line numbers on which it occurs. Remove noise words like “the”, “and”, and so on.

    Solution to Exercise 6-3

  4. Exercise 6-4. Write a program that prints the distinct words in its input sorted into decreasing order of frequency of occurrence. Precede each word by its count.

    Solution to Exercise 6-4

  5. Exercise 6-5. Write a function undef that will remove a name and definition from the table maintained by lookup and install.

    Solution to Exercise 6-5

  6. Exercise 6-6. Implement a simple version of the #define processor (i.e., no arguments) suitable for use with C programs, based on the routines of this section. You may also find getch and ungetch helpful.

    Solution to Exercise 6-6

See also: K&R C — All Chapters Index | Complete C Programs List

1 comment on “K&R C Chapter 6 Exercise Solutions — Structures

  • Audrey Liew says:

    Every single code We try either leaves an enormous gap towards the top of the web page, or at the end of the web page. Its certainly that code causing this.. I’ve attempted so many rules trying to conceal the websites & extended network, but they all keep a white gap either at the top or at the bottom.. Anyone got one that doesnt cause the white gap? Or how to take away the white space?.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>