K&R Exercises

Exercise 4-12: Recursive itoa

Adapt the ideas of printd to write a recursive version of itoa; that is, convert an integer into a string by calling a recursive routine.

#include <stdio.h>

/* printd:  print n in decimal */
void printd(int n)
{
    if (n < 0) {
        putchar('-');
        n = -n;
    }
    if (n / 10)
        printd(n / 10);
    putchar(n % 10 + '0');
}

Each input line holds an integer that fits in an int. Print the string itoa makes from it on a line of its own.

Test Cases

· space→ tab↵ newline

Submissions

No submissions yet