k&r; exercise 1.6 - rewrite with tests