Provide high quality solutions to the exercises in "The C Programming Language" by Kernighan and Ritchie (2nd edition), also referred to as "K&R C Bible".
- Chapter 1. A Tutorial Introduction
- Chapter 2. Types, Operators, and Expressions
- Chapter 3. Control Flow
- Chapter 4. Functions and Program Structure
- Chapter 5. Pointers and Arrays
- Exercise 1-9. Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank. solution
- Exercise 1-10. Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b, and each backslash by \\. This makes tabs and backspaces visible in an unambiguous way. solution
- Exercise 1-12. Write a program that prints its input one word per line. solution
- Exercise 1-13. Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientations is more challenging. solution
- Exercise 1-14. Write a program to print a histogram of the frequencies of different characters in its input. solution
- Exercise 1-15. Rewrite the temperature conversion program of Section 1.2 to use a function for conversion. solution
- Exercise 1-16. Revise the main routine of the longest-line program so it will correctly print the lenght of arbitrarily long input lines, and as much as possilbe of the text. solution
- Exercise 1-17. Write a program to print all input lines that are longer than 80 characters. solution
- Exercise 1-18. Write a program to remove trailing blanks and tabs from each line of input, and to delete entriely blank lines. solution
- Exercise 1-19. Write a function
reverse(s)that reverses the character strings. Use it to write a program that reverses its input a line at a time. solution - Exercise 1-20. Write a program
detabthat replaces tabs in the input with the proper number of blanks to space to the next tab stop. Assume a fixed set of tab stops, say everyncolumns. Shouldnbe a variable or symbolic parameter? solution - Exercise 1-21. Write a program
entabthat replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing. Use the same tab stops as fordetab. When either a tab or a single blank would suffice to reach a tab stop, which should be given preference? solution - Exercise 1-22. Write a program to "fold" long input lines into two or more shorter lines after the last non-blank character that occurs before the
n-th column of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column. solution - Exercise 1-23. Write a program to remove all comments from a C program. Don't forget to handle quoted strings and character constants properly. C comments do not nest. solution
- Exercise 1-24. Write a program to check a C program for rudimentary syntax errors like unbalanced parentheses, brackets and braces. Don't forget about quotes, both single and double, escape sequences, and comments. (This program is hard if you do it in full generality.) solution
- Exercise 2-1. Write a program to determine the ranges of
char,short,int, and long variables, bothsignedandunsigned, by printing appropriate values from standard hearders and by direct computation. Harder if you compute them: determine the ranges of the various floating-point types. solution - Exercise 2-2. Write a loop equivalent to the
forloop above without using&&or||. solution - Exercise 2-3. Write the function
htoi(s), which converts a string of hexadecimal digits (including an optional0xor0X) into its equivalent integer value. The allowable digits are0through9,athroughf, andAthroughF. solution - Exercise 2-4. Write an alternate version of
squeeze(s1, s2)that deletes each character ins1that matches any character in the strings2. solution - Exercise 2-5. Write the function
any(s1, s1), which returns the first location in the strings1where any character from the strings2occurs, or-1ifs1contains no characters froms2. (The standard library functionstrpbrkdoes the same job but returns a pointer to the location.) solution - Exercise 2-6. Write a function
setbits(x, p, n, y)that returnsxwith thenbits that begin at positionpset to the rightmostnbits ofy, leaving the other bits unchanged. solution - Exercise 2-7. Write a function
invert(x, p, n)that returnxwith thenbits that begin at positionpinverted (i.e., 1 changed into 0 and vice versa), leaving the others unchanged. solution - Exercise 2-8. Write a function
rightrot(x, n)that returns the value of the integerxrotated to the right bynbit positions. solution - Exercise 2-9. In a two's complement number system,
x &= (x - 1)deletes the rightmost 1-bit inx. Explain why. Use this observation to write a faster version ofbitcount. solution - Exercise 2-10. Rewrite the function
lower, which converts the upper case letters to lower case, with a conditional expression instead ofif-else. solution
- Exercise 3-1. Our binary search makes two tests inside the loop, when one would suffice (at the price of more tests outside). Write a version with only one test inside the loop and measure the difference in run-time. solution
- Exercise 3-2. Write a function
escape(s, t)that converts characters like newline and tab into visible escape sequences like\nand\tas it copies the stringttos. Use aswitch. Write a function for the other direction as well, coverting escape sequences into the real characters. solution - Exercise 3-3. Write a function
expand(s1, s2)that expands shorhand notations likea-zin the strings1into the equivalent complete listabc...xyzins2. Allow for letters of either case and digits, and be prepared to handle cases likea-b-canda-z0-9and-a-z. Arrange that a leading or trailing-is taken literally. solution - Exercise 3-4. In a two's complement number representation, our version of
itoadoes not handle the largest negative number, that is, the value ofnequal to-(2^{wordsize-1}). Explain why not. Modify it to print that value correctly, regardless of the machine on which it runs. solution - Exercise 3-5. Write the function
itob(n, s, b)that converts the integerninto a basebcharacter representation in the strings. In particalar,itob(n, s, 16)formatsnas a hexadecimal integer ins. solution - Exercise 3-6. Write a version of
itoathat accepts three arguments instead of two. The third argument is a minimum field width; the converted number must be padded with blanks on the left if necessary to make it wide enough. solution
- Exercise 4-1. Write the function
strrindex(s, t), which returns the position of the rightmost occurrence oftins, or-1if there is none. solution - Exercise 4-2. Extend
atofto handle scientific notation of the form123.45e-6where a floating-point number may be followed byeorEand an optionally signed exponent. solution - Exercise 4-3. Given the basic framework, it's straightforward to extend the calculator. Add the modulus (
%) and provisions for negative numbers. solution - Exercise 4-4. Add commands to print the top element of the stack without poping, to duplicate it, and to swap the top two elements. Add a command to clear the stack. solution
- Exercise 4-5. Add access to library functions like
sin,expandpow. See<math.h>in Appendix B, Section 4. solution - Exercise 4-6. Add commands for handling variables. (It's easy to provide twenty-six variables with single-letter names.) Add a variable for the most recently printed value.
- Exercise 4-7. Write a routine
ungetsthat will push back an entire string onto the input. Shouldungetsknow aboutbufandbufp, or should it just useungetch? - Exercise 4-8. Supppose that there will never be more than one character of pushback. Modify
getchandungetchaccordingly. - Exercise 4-9. Our
getchandungetchdo not handle a pushed-backEOFcorrectly. Decide what their properties ougth to be if anEOFis pushed back, then implement your design. - Exercise 4-10. An alternative organization uses
getlineto read an entire input line; this makesgetchandungetchunnecessary. Revise the calculator to use this appproach. - Exercise 4-11. Modify
getopso that it doesn't need to useungetch. Hint: use an internalstaticvariable. solution - Exercise 4-12. Adapt the ideas of
printdto write a recursive version ofitoa; that is, convert an integer into a string by calling a recursive routine. solution - Exercise 4-13. Write a recursive version of the function
reverse(s), which reverse the stringsin place. solution - Exercise 4-14. Define a macro
swap(t, x, y)that interchanges two arguments of typet. (Block structure will help.) solution
- Exercise 5-1. As written,
getinttreats a+or-not followed by a digit as a valid representation of zero. Fix it to push such a character back on the input. solution - Exercise 5-2. Write
getfloat, the floating-point analog ofgetint. What type doesgetfloatreturn as its function value? solution - Exercise 5-3. Write a pointer version of the function
strcatthat we showed in Chapter 2:strcat(s, t)copies the stringtto the end ofs. - Exercise 5-4. Write the function
strend(s, t), which returns 1 if the stringtoccurs at the end of the strings, and zero otherwise. - Exercise 5-5. Write the version of the library functions
strncpy,strncat, andstrncmp, which operate on at most the firstncharacters of their arguement strings. For example,strncpy(s, t, n)copies at mostncharacters ofttos. Full descriptions are in Appendix B. - Exercise 5-6. Rewrite appropriate programs from earlier chapters and exercises with ponters instead of array indexing. Good possibilites include
getline(Chapter 1 and 4),atoi,itoa, and their variants (Chapter 2, 3, and 4),reverse(Chapter 3), andstrindexandgetop(Chapter 4). - Exercise 5-7. Rewrite
readlinesto store lines in an array supplied bymain, rather than callingallocto maintain storage. How much faster is the program? solution - Exercise 5-8. There is no error checking in
day_of_yearormonth_day. Remedy this defect. solution - Exercise 5-9. Rewrite the routines
day_of_yearandmonth_daywith pointers instead of indexing. solution