-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
55 lines (50 loc) · 1.39 KB
/
ft_itoa.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: johnavar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/05 12:40:11 by johnavar #+# #+# */
/* Updated: 2024/02/05 18:08:49 by sebasnadu ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../include/libft.h"
static size_t nbr_len(int nbr)
{
int len;
len = 0;
if (nbr <= 0)
len++;
while (nbr)
{
len++;
nbr = nbr / 10;
}
return (len);
}
char *ft_itoa(int n)
{
long nbr;
size_t len;
char *str;
len = nbr_len(n);
str = (char *)malloc(sizeof(char) * len + 1);
if (!str)
return (NULL);
nbr = n;
if (nbr < 0)
{
str[0] = '-';
nbr *= -1;
}
if (nbr == 0)
str[0] = '0';
str[len--] = '\0';
while (nbr != 0)
{
str[len--] = (nbr % 10) + '0';
nbr = nbr / 10;
}
return (str);
}