I wrote following function to convert integer to string and handle largest negative number as well.But it is not working.Could someone tell me what I am doing wrong?? (n is number,char s[] is str and maxsize is sizeof(str))
The code is like thathttp://www.learntosolveit.com/cprogramming/Ex_3.4_itoa-2.html
I just changed itoa from void to int and with 3 parameters instead of 2.maxsize is sizeof(str)
int itoa(int n,char s[],int maxsize) // itoa asking for trouble unless you
{ //have it in a namespace
int i,sign,digit;
char p[]="0123456789";
sign=n;
i=0;
while (i < msize-1) // msize? maxsize probably
{
digit=n%10;
n/=10;
s[i]=p[digit];
i++;
}
if (sign < 0 && i < msize-1) // msize? maxsize probably
s[i]='-';
i++;
// the else is orphaned need {} round the if path
elseif (i >= msize-1) // msize? maxsize probably
return -1;
s[i]='\0';
reverse(s);
return 0;
}
You would probably want to control the while loop with n rather than the size of the array...but make sure you don't write out of bounds if the array is too small.
http://www.cplusplus.com/reference/cstdlib/itoa/
This function (itoa) is not defined in ANSI-C and is not part of C++, but is supported by some compilers.