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-10

Exercise 5-10. Write the program expr, which evaluates a reverse Polish expression from the command line, where each operator or operand is a separate argument. For example, expr 2 3 4 + * evaluates 2 * (3+4). The K&R RPN calculator from Chapter 4 reads from stdin. This version reads from argv instead — each …

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-8

Exercise 5-8. There is no error checking in day_of_year or month_day. Remedy this defect. K&R’s Section 5.8 defines a 2D array daytab[2][13] for days per month and two functions that convert between day-of-year and (month, day). Neither validates its inputs. Bad values (year 0, month 13, day 367) cause silent wrong results or array overruns. …