August 12, 2011
Ex 3-6

Write a version of itoa that accepts three arguments instead of two. The third argument is a minimum field width; the converted number must be padded with blanks on the left if necessary to make it wide enough.

void itoa(int n, char s[], )
{
        int i, sign;

        sign = n;

        i = 0;
        do{
        s[i++] = n % 10 + ‘0’;
        } while ((n /= 10) != 0);

        if(sign < 0)
                s[i++] = ‘-‘;

        while(i < w)
                s[i++] = ’ ‘;
   
        s[i] = ‘\0’;

        reverse(s);

}

3:14am  |   URL: http://tmblr.co/Zi3hLy8DOc45
Filed under: K&amp;R