ft_itoa.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* ft_itoa.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@students.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2015/11/25 15:33:13 by bchanot #+# #+# */
  9. /* Updated: 2016/05/23 15:36:03 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "libft.h"
  13. #include <stdlib.h>
  14. char *ft_itoa(long long int n)
  15. {
  16. int size;
  17. char *str;
  18. if (n == -2147483648)
  19. return (ft_strdup("-2147483648"));
  20. size = ft_nbrlen(n);
  21. if (!(str = (char *)malloc(sizeof(char *) * (size + 1))))
  22. return (NULL);
  23. if (n < 0)
  24. {
  25. str[0] = '-';
  26. n = -n;
  27. }
  28. str[size] = '\0';
  29. while (n >= 10)
  30. {
  31. str[size - 1] = n % 10 + 48;
  32. n = n / 10;
  33. size--;
  34. }
  35. str[size - 1] = n + 48;
  36. return (str);
  37. }