78 lines
1.7 KiB
C
78 lines
1.7 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_itoa.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/11/07 13:06:35 by dgaillet #+# #+# */
|
|
/* Updated: 2025/11/09 18:47:18 by dgaillet ### ########lyon.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "libft.h"
|
|
|
|
static size_t count_digits(int n)
|
|
{
|
|
size_t size;
|
|
|
|
size = 0;
|
|
if (n <= 0)
|
|
size++;
|
|
while (n)
|
|
{
|
|
n /= 10;
|
|
size++;
|
|
}
|
|
return (size);
|
|
}
|
|
|
|
static void insert_char(char *str, unsigned int nbr, size_t index)
|
|
{
|
|
while (nbr)
|
|
{
|
|
str[index--] = nbr % 10 + '0';
|
|
nbr /= 10;
|
|
}
|
|
}
|
|
|
|
char *ft_itoa(int n)
|
|
{
|
|
unsigned int nbr;
|
|
char *str;
|
|
size_t size;
|
|
|
|
nbr = n;
|
|
if (n < 0)
|
|
nbr = n * -1;
|
|
size = count_digits(n);
|
|
str = malloc(sizeof(char) * (size + 1));
|
|
if (!str)
|
|
return (NULL);
|
|
str[size] = '\0';
|
|
if (nbr == 0)
|
|
str[0] = '0';
|
|
else
|
|
{
|
|
if (n < 0)
|
|
str[0] = '-';
|
|
insert_char(str, nbr, size - 1);
|
|
}
|
|
return (str);
|
|
}
|
|
/*
|
|
int main(int argc, char **argv)
|
|
{
|
|
char *str;
|
|
|
|
if (argc > 1)
|
|
{
|
|
str = ft_itoa(42);
|
|
if (!str)
|
|
return (1);
|
|
ft_putstr_fd(str, 1);
|
|
free(str);
|
|
}
|
|
}
|
|
*/
|