/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_itoa.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:12:52 by erey-bet #+# #+# */ /* Updated: 2022/10/26 16:00:48 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" static void ft_itoa_bis(char *str, int len, int i, long nl) { while (len > 1) { len--; str[i] = (nl / ft_power(10, len) % 10) + 48; i++; } str[i] = nl % 10 + 48; i++; str[i] = '\0'; } char *ft_itoa(int n) { char *str; int len; int i; long nl; nl = n; if (nl < 0) nl *= -1; len = ft_get_size(nl); i = 0; if (n < 0) { str = malloc(len + 2); if (str == NULL) return (NULL); str[i] = '-'; i++; } else str = malloc(len + 1); if (str == NULL) return (NULL); ft_itoa_bis(str, len, i, nl); return (str); }