CSM217: C for Computer Scientists

Lecture 2: Exercises

Exercise 1

Modify the temperature conversion program to print the table in reverse order, that is from 300 degrees to 0, using a for loop.

The solution is here, or else you can mail me.

Exercise 2

Write a program to copy its input to its output, replacing each sequence of two or more blanks with a single blank.

The solution is here, or else you can mail me.

Exercise 3

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, backspaces and backslashes visible in an unambiguous way.

The solution is here, or else you can mail me.

Exercise 4

Write a program to print a horizontal histogram of the frequencies of different characters in its input. To keep the problem a little bit simple, you can ignore the differences between uppercase and lowercase letter, and you can also ignore non-alphabetic characters in the input. Your output should be something like:

4 x   x  x  x   x   x   x  
3 xx  x xx  x   x x xx xxx
2 xx xxxxxx x x xxx xxxxxx
1 xxxxxxxxxxx xxxxx xxxxxx
  abcdefghijklmnopqrstuvwxyz

Use a 26-element array to store the counts of the observed characters, rather than 26 variables!

The solution is here, or else you can mail me.