/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_itoa.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: grobledo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/19 19:32:47 by grobledo #+# #+# */ /* Updated: 2023/02/19 19:32:48 by grobledo ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" int countsize(int nb) { long int temp; int size; size = 0; temp = nb; if (nb < 0) { temp = -nb; size = size +1; } while (temp > 9) { temp = (temp / 10); size++; } size++; return (size); } char *ft_itoa(int nb) { long int temp; char *res; int size; if (nb == -2147483648) return (ft_strdup("-2147483648")); temp = nb; size = countsize(nb); res = ft_calloc(size + 1, sizeof (char)); if (!res) return (NULL); if (nb < 0) { res[0] = '-'; temp *= -1; } if (nb == 0) res[0] = '0'; while (temp > 0) { res[size - 1] = (temp % 10 + 48); size--; temp /= 10; } return (res); }