COS 126
Exercise Set 5
Answers

These programming exercises are intended help review the material on recursion and character pointers. Do not turn in solutions.

  1. Reimplement the recursive version of int itoa(int n, int b, char *str) from Exercise Set 4 using only pointers. Recall that itoa fills str with a null-terminated string giving the character representation of n in base b and returns the length of this string. For example:
    char buf[20];
    k = itoa(875, 5, buf);
    printf("%s\n", buf);
    Sets k to 5 and prints 12001.
  2. Reimplement a completely nonrecursive version of itoa using pointers.

Answers

Try to implement these programs before looking at the suggested solutions.

  1. itoa2.c (recursive)
  2. itoa3.c (nonrecursive)

Copyright © 1996 David R. Hanson / drh@cs.princeton.edu
$Revision: 1.2 $ $Date: 1996/10/23 13:05:31 $