Function itoa - what is wrong??

Pages: 12
A Google search with the term wiki is useful.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int itoa(int n, char* s, int msize)
{
    int i = 0;
    int digit = 0;
    
    if ( n < 0 )
    {
        s[0] = '-';
        n *= -1;
        i++;
    }
    
    while (n > 0)
    {
        digit=n%10;
        n/=10;
        s[i]= digit + '0';
        i++;
    }
    
    s[i]='\0';
    //reverse(s);
    
    return 0;
}


All you need now is some error check on msize overrun and reverse, also might need to adjust placing of sign lines in the sequence. Too trivial for me so make it an exercise for OP. Also need to cater for zero, another exercise for OP :)
Last edited on
Topic archived. No new replies allowed.
Pages: 12