K&R C Programs Exercise 6-5

Exercise 6-5. Write a function undef that will remove a name and definition from the table maintained by lookup and install. K&R Section 6.6 presents a hash table with separate chaining: an array of HASHSIZE buckets, each being the head of a linked list of struct nlist nodes. install adds a name/definition pair; lookup finds …

K&R C Programs Exercise 6-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. Count words using K&R’s binary tree (Section 6.5), then collect all nodes into an array and sort by count descending using qsort. The key insight: the tree is …

K&R C Programs Exercise 6-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 from the list. The data structure is a binary tree where each node holds the word and …

K&R C Programs Exercise 6-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. The strategy: Use the improved getword from Exercise 6-1 to read identifiers, …

K&R C Programs Exercise 6-1

Exercise 6-1. Our version of getword does not properly handle underscores, string constants, comments, or preprocessor control lines. Write a better version. K&R’s getword from Section 6.3 reads the next identifier or non-alphabetic character from input, but has four gaps: Underscores: valid in C identifiers (_var, size_t) but the original checks only isalpha String constants: …

K&R C Programs Exercise 5-20

Exercise 5-20. Expand dcl to handle declarations with function argument types, qualifiers like const, and so on. The basic dcl treats () as “function returning” but ignores argument types. This exercise adds: parameter lists inside (), type qualifiers (const, volatile), storage classes (static, extern), and the void type. The approach: when ( is encountered and …