The K&R Index of Blog Entries


The book “The C Programming Language” by Brian Kernighan and Dennis Ritchie is a seminal book on the C and C++ computer languages. The book introduced the C language to the world and is referred to as K&R. It’s not a reference manual but a introduction to the language where the writers explain by example. They present code to solve a problem and write about what the code is doing. As they write,

“The only way to learn a new programming language is by writing programs in it.”

I will work my way through the sample code and blog about. I’m using the 2nd edition published in 1988. The language and technology has changed since, but that will be part of the blog. The blog entries will all have the tag K+R. The web site won’t accept the ampersand as a character.

Why do it? There’s no one reason, but several. To learn the language. To show others I know something about the C language. To create notes for future reference where, on a blog, I can easily find an answer on something I did previously. To practice my writing of documentation and in general.

I am using Visual C++ 2010 and creating the code as a console application. At some point I will try other compilers, but that’s for another day. 

The List with Links
 


Exercise 1-1. Run the “hello, world” program on your system. Experiment with leaving out parts of the program, to see what error messages you get.

Exercise 1-2. Experiment to find out what happens when prints's argument string contains \c, where c is some character not listed above.


Exercise 1-3. Modify the temperature conversion program to print a heading above the table.

Exercise 1-4. Write a program to print the corresponding Celsius to Fahrenheit table.


Exercise 1-5. Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.





Exercise 1-6. Verify that the expression getchar() != EOF is 0 or 1.

Exercise 1-7. Write a program to print the value of EOF.



Exercise 1-8. Write a program to count blanks, tabs, and newlines.


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.


Exercise 1-09. Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.



Exercise 1-12. Write a program that prints its input one word per line.


Let us write a program to count the number of occurrences of each digit, of white space characters (blank, tab, newline), and of all other characters.


Exercise 1-14. Write a program to print a histogram of the frequencies of different characters in its input.


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 orientation is more challenging.



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 orientation is more challenging.


Exercise 1.15. Rewrite the temperature conversion program of Section 1.2 to use a function for conversion.




Exercise 1-16. Revise the main routine of the longest-line program so it will correctly print the length of arbitrary long input lines, and as much as possible of the text.

Exercise 1-17. Write a program to print all input lines that are longer than 80 characters.

Exercise 1-18. Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.



Exercise 1-19. Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input a line at a time.









No comments:

Post a Comment