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. …

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), …