Thread: Integer to String (itoa)

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    38

    Cool Integer to String (itoa)

    Hello cboarders!
    I have written the below function, to convert integers to a character string.

    The output correctly shows negative numbers. For positive numbers however, the string is not ending with a '\0'. I don't understand why it is not outputting with a '\0'.

    1. Why is the '\0' not showing for positive numbers?
    2. I have been asked to return "NULL" if the memory allocation fails. How do I know when the memory allocation has failed?

    Thanks kindly for your help or guidance to get me on the right track!

    Code:
    char    *ft_itoa(int n)
    {
        char    *str;
        int     digits;
        int     number;
        int     neg_flag;
        char    ip;
    
    
        digits = 0;
        neg_flag = 0;
        if (n < 0)
        {
            n = -1 * n;
            neg_flag = 1;
        }
        number = n;
        while(number > 0)
        {
            number = number/10;
            digits++;
        }
        if (neg_flag)
            digits = digits + 1;
        str = (char*)malloc(sizeof(char)*(digits + 1));
        str[digits + 1] = '\0';
        number = n;
        while (number)
        {
            ip = number % 10 + '0';
            str[digits - 1] = ip;
            digits--;
            number = number/10;
        }
        if (neg_flag)
            str[0] = '-';
        return (str);
    }
    MAIN:
    Code:
    #include <stdio.h>#include <string.h>
    
    
    int main () {
        int n;
    
    
        n = 20;
        printf("RESULT:\n%s",ft_itoa(n));
    
    
       return(0);
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
        str = (char*)malloc(sizeof(char)*(digits + 1));
        str[digits + 1] = '\0';
    Say that there is a 4 digit number. Malloc(5) would be correct, because you need 5 positions: four for the digits and 1 for the terminating zero.

    If we think about the result we want, then we might come up with this.
    Code:
    position:    0    1    2    3     4  
    string:   { '1', '2', '3', '4', '\0' }
    But you used digits + 1 instead which puts the '\0' in postion 5, the wrong place for positive numbers.

    When the number is negative, the result should be this.
    Code:
    position:    0    1    2    3     4    5
    string:   { '-', '1', '2', '3', '4', '\0' }
    So that is why it is appearing correct for negative numbers.
    Last edited by whiteflags; 10-29-2017 at 09:56 AM.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    38
    Ahh crystal clear now! thanks!!! Got confused with the digits and the positions in the string.. Thank you kindly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert Integer to String and String to Integer
    By hqt in forum C++ Programming
    Replies: 26
    Last Post: 09-15-2011, 11:39 AM
  2. Convert int to String (no itoa)
    By Maga in forum C Programming
    Replies: 3
    Last Post: 03-24-2010, 03:42 PM
  3. Replies: 4
    Last Post: 12-04-2009, 10:22 AM
  4. Put an integer in a string?
    By guitarist809 in forum C++ Programming
    Replies: 1
    Last Post: 04-18-2008, 12:24 PM
  5. integer to constant char w/o using itoa()
    By kes103 in forum C++ Programming
    Replies: 1
    Last Post: 01-06-2003, 10:42 PM

Tags for this Thread