"The C Programming Language", 2nd edition, Kernighan and Ritchie

Answers to Exercises

Chapter 5 - Pointers and Arrays


Ex No Pg No Exercise Solution Solved by
1 97 As written, getint treats a + or - not followed by a digit as a valid representation of zero. Fix it to push such a character back on the input. Listing krx501 Gregory Pietsch
2 97 Write getfloat , the floating-point analog of getint . What type does getfloat return as its function value? Listing krx502 Chris Mears, Gregory Pietsch
3 107 Write a pointer version of the function strcat that we showed in Chapter 2: strcat(s,t) copies the string t to the end of s . Listing krx503 Me
4 107 Write the function strend(s,t) , which returns 1 if the string t occurs at the end of the string s , and zero otherwise. Listing krx504 Bryan Williams
5 107 Write versions of the library functions strncpy , strncat , and strncmp , which operate on at most the first n characters of their argument strings. For example, strncpy(s,t,n) copies at most n characters of t to s . Full descriptions are in Appendix B. Listing krx505 Lars Wirzenius
6 107 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, and 4), reverse (Chapter 3), and strindex and getop (Chapter 4). Listing krx506 Gregory Pietsch
7 110 Rewrite readlines to store lines in an array supplied by main , rather than calling alloc to maintain storage. How much faster is the program? Listing krx507 Steven Huang
8 112 There is no error-checking in day_of_year or month_day. Remedy this defect. Listing krx508 Lars Wirzenius
9 114 Rewrite the routines day_of_year and month_day with pointers instead of indexing. Listing krx509 Lars Wirzenius, Gregory Pietsch
10 118 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 X (3+4).
Listing krx510 Lars Wirzenius
11 118 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. Listing krx511 Gregory Pietsch
12 118 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.
No solution yet posted. ----
13 118 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. Write the program so it makes the best use of available storage; lines should be stored as in the sorting program of Section 5.6, not in a two-dimensional array of fixed size.
Listing krx513 Gregory Pietsch, Steven Huang
14 121 Modify the sort program to handle a -r flag, which indicates sorting in reverse (decreasing) order. Be sure that -r works with -n. Listing krx514 Steven Huang
15 121 Add the option -f to fold upper and lower case together, so that case distinctions are not made during sorting; for example, a and A compare equal. No solution yet posted. ----
16 121 Add the -d ("directory order") option, which makes comparisons only on letters, numbers and blanks. Make sure it works in conjunction with -f . No solution yet posted. ----
17 121 Add a field-handling capability, so sorting may be done on fields within lines, each field sorted according to an independent set of options. (The index for this book was sorted with -df for the index category and -n for the page numbers.) No solution yet posted. ----
18 126 Make dcl recover from input errors. No solution yet posted. ----
19 126 Modify undcl so that it does not add redundant parentheses to declarations. No solution yet posted. ----
20 126 Expand dcl to handle declarations with function argument types, qualifiers like const , and so on. No solution yet posted. ----


Back
Home




You are visitor number - call again soon!