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
detab
that 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 everyn
columns. Shouldn
be a variable or symbolic parameter? solution - Exercise 1-21. Write a program
entab
that 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, bothsigned
andunsigned
, 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
for
loop above without using&&
or||
. solution - Exercise 2-3. Write the function
htoi(s)
, which converts a string of hexadecimal digits (including an optional0x
or0X
) into its equivalent integer value. The allowable digits are0
through9
,a
throughf
, andA
throughF
. solution - Exercise 2-4. Write an alternate version of
squeeze(s1, s2)
that deletes each character ins1
that matches any character in the strings2
. solution - Exercise 2-5. Write the function
any(s1, s1)
, which returns the first location in the strings1
where any character from the strings2
occurs, or-1
ifs1
contains no characters froms2
. (The standard library functionstrpbrk
does the same job but returns a pointer to the location.) solution - Exercise 2-6. Write a function
setbits(x, p, n, y)
that returnsx
with then
bits that begin at positionp
set to the rightmostn
bits ofy
, leaving the other bits unchanged. solution - Exercise 2-7. Write a function
invert(x, p, n)
that returnx
with then
bits that begin at positionp
inverted (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 integerx
rotated to the right byn
bit 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\n
and\t
as it copies the stringt
tos
. 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-z
in the strings1
into the equivalent complete listabc...xyz
ins2
. Allow for letters of either case and digits, and be prepared to handle cases likea-b-c
anda-z0-9
and-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
itoa
does not handle the largest negative number, that is, the value ofn
equal 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 integern
into a baseb
character representation in the strings
. In particalar,itob(n, s, 16)
formatsn
as a hexadecimal integer ins
. solution - Exercise 3-6. Write a version of
itoa
that 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 oft
ins
, or-1
if there is none. solution - Exercise 4-2. Extend
atof
to handle scientific notation of the form123.45e-6
where a floating-point number may be followed bye
orE
and 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
,exp
andpow
. 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
ungets
that will push back an entire string onto the input. Shouldungets
know aboutbuf
andbufp
, or should it just useungetch
? - Exercise 4-8. Supppose that there will never be more than one character of pushback. Modify
getch
andungetch
accordingly. - Exercise 4-9. Our
getch
andungetch
do not handle a pushed-backEOF
correctly. Decide what their properties ougth to be if anEOF
is pushed back, then implement your design. - Exercise 4-10. An alternative organization uses
getline
to read an entire input line; this makesgetch
andungetch
unnecessary. Revise the calculator to use this appproach. - Exercise 4-11. Modify
getop
so that it doesn't need to useungetch
. Hint: use an internalstatic
variable. solution - Exercise 4-12. Adapt the ideas of
printd
to 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 strings
in 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,
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. solution - Exercise 5-2. Write
getfloat
, the floating-point analog ofgetint
. What type doesgetfloat
return as its function value? solution - Exercise 5-3. Write a pointer version of the function
strcat
that we showed in Chapter 2:strcat(s, t)
copies the stringt
to the end ofs
. - Exercise 5-4. Write the function
strend(s, t)
, which returns 1 if the stringt
occurs 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 firstn
characters of their arguement strings. For example,strncpy(s, t, n)
copies at mostn
characters oft
tos
. 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), andstrindex
andgetop
(Chapter 4). - Exercise 5-7. Rewrite
readlines
to store lines in an array supplied bymain
, rather than callingalloc
to maintain storage. How much faster is the program? solution - Exercise 5-8. There is no error checking in
day_of_year
ormonth_day
. Remedy this defect. solution - Exercise 5-9. Rewrite the routines
day_of_year
andmonth_day
with pointers instead of indexing. solution