K&R C Programs Exercise 5-13

Exercise 5-13. Write the program tail, which prints the last n lines of its input. By default, n is 10, say, but it can be changed by an optional argument, so that tail -n prints the last n lines. The program should behave rationally no matter how unreasonable the input or the value of n. …

K&R C Programs Exercise 5-12

Exercise 5-12. Extend entab and detab to accept the shorthand entab -m +n to mean tab stops every n columns, starting at column m. Choose convenient (for the user) default behavior. Two new argument forms extend the programs from Exercise 5-11: -m sets the starting column and +n sets the interval. So detab -4 +3 …

K&R C Programs Exercise 5-11

Exercise 5-11. Modify the programs entab and detab (written as exercises in Chapter 1) to accept a list of tab stops as arguments. Use the default tab settings if there are no arguments. The Chapter 1 versions of entab and detab used a fixed tab width of 8. This version reads tab stop positions from …

K&R C Programs Exercise 5-9

Exercise 5-9. Rewrite the routines day_of_year and month_day with pointers instead of indexing. Replace every daytab[leap][i] with pointer arithmetic. The key step: instead of indexing into the 2D array, take a pointer to the correct row (const char *p = daytab[ly]) and use p[i] or *(p + i) to access elements. The loop variable can …

K&R C Programs Exercise 5-7

Exercise 5-7. Rewrite readlines to store lines in an array supplied by main, rather than calling alloc to maintain storage itself. How much faster is the program? The K&R readlines from Section 5.6 calls a custom alloc to carve space out of a static buffer. This exercise moves storage to a flat char linestore[] array …

K&R C Programs Exercise 5-6

Exercise 5-6. Rewrite appropriate programs from earlier chapters and exercises with pointers instead of array indexing. Good possibilities include getline (Chapters 1 and 4), atoi, itoa, and their variants (Chapters 2, 3, 4), reverse (Chapter 3), strindex and getop (Chapter 4). This exercise is about translation: every array subscript a[i] can be written as *(a+i), …