/* Exercise 4.12 from "The C programming language" book by K&R */
#include <stdio.h>
#include <string.h>
void reverse(char s[])
{
int c, i, j;
for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
/* recursive version of itoa() */
void itoa_r(char n, char s[])
{
static int i;
static int sign;
if (i == 0)
if ((sign = n) < 0)
n = -n;
s[i++] = n % 10 + '0';
if ((n /= 10) > 0)
itoa_r(n,s);
else {
if (sign < 0) {
s[i++] = '-';
sign = 0;
}
s[i] = '\0';
}
}
int main(void)
{
char s[100] = "";
itoa_r(-123,s);
reverse(s);
printf("%s\n",s);
return 0;
}
----------