K & R C Programs Exercise 3-5.

K and R C, Solution to Exercise 3-5:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
Write a c program to convert the integer n into a base b character representation in the string s. In particular, itob(n, s, 16) formats n as a hexadecimal integer in s. Read more about C Programming Language .
/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdlib.h>
#include <stdio.h>

void itob(int n, char s[], int b);
void reverse(char s[]);

int main(void) {
char buffer[10];
int i;

for ( i = 2; i <= 20; ++i ) {
itob(255, buffer, i);
printf("Decimal 255 in base %-2d : %sn", i, buffer);
}
return 0;
}


/* itob: convert n to charecters in s - base b */

void itob(int n, char s[], int b) {

int i, j, sign;
void reverse(char s[]);


if ((sign = n) < 0)
n = -n;
i = 0;
do {
j = n%b;
s[i++] = (j <= 9) ? j+'0' : j+'a'-10;
} while ((n /= b) > 0);
if (sign < 0)
s[i++] = '-';
s[i] = '