mirror of
https://github.com/Ladebeze66/libft.git
synced 2025-12-13 04:36:55 +01:00
57 lines
1.4 KiB
C
Executable File
57 lines
1.4 KiB
C
Executable File
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_itoa.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/02/17 15:53:35 by fgras-ca #+# #+# */
|
|
/* Updated: 2023/02/21 10:21:07 by fgras-ca ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "libft.h"
|
|
|
|
static size_t ft_len_nb(int nb)
|
|
{
|
|
int len;
|
|
|
|
len = 0;
|
|
if (nb <= 0)
|
|
len++;
|
|
while (nb)
|
|
{
|
|
len++;
|
|
nb = nb / 10;
|
|
}
|
|
return (len);
|
|
}
|
|
|
|
char *ft_itoa(int n)
|
|
{
|
|
int len;
|
|
char *str;
|
|
long nb;
|
|
|
|
len = ft_len_nb(n);
|
|
nb = n;
|
|
str = malloc(sizeof(char) * len +1);
|
|
if (!str)
|
|
return (0);
|
|
if (nb < 0)
|
|
{
|
|
str[0] = '-';
|
|
nb = -nb;
|
|
}
|
|
if (nb == 0)
|
|
str[0] = '0';
|
|
str[len--] = '\0';
|
|
while (nb)
|
|
{
|
|
str[len] = nb % 10 + '0';
|
|
len--;
|
|
nb = nb / 10;
|
|
}
|
|
return (str);
|
|
}
|