void myitoa(int n, char *s)
{
int i, sign;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
if (n / 10)
myitoa(n / 10, *s + 1);
s[i++] = n % 10 + '0';
if (sign < 0)
s[i++] = '-';
}
// myitoa(n / 10, *s + 1); // line 12
myitoa( n / 10, s + 1 );
The logic for placing the sign is broken. And c-style strings need to be null-terminated.
Something like this, perhaps:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int myitoa( int n, char* s )
{
int i = 0 ;
if( n < 0 )
{
s[i++] = '-' ; /* record sign */
++s ;
n = -n; /* make n positive */
}
if( n/10 ) i += myitoa( n/10, s );
s[i++] = n % 10 + '0';
s[i] = 0 ; // null-terminate
return i ; // return number of chars used
}