-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
40 lines (37 loc) · 1.3 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: stales <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/04 18:53:30 by stales #+# #+# */
/* Updated: 2022/04/12 14:45:13 by stales ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa(int n)
{
long int nb_size;
long int nb_long;
char *nb_str;
nb_size = ft_nbrlen(n);
nb_str = ft_calloc(1, nb_size + 1);
if (!nb_str)
return (NULL);
nb_long = (long int)n;
if (nb_long < 0)
{
nb_long = ~(nb_long - 1);
nb_str[0] = '-';
}
if (nb_long == 0)
nb_str[0] = '0';
while (nb_long)
{
nb_str[nb_size - 1] = '0' + (nb_long % 10);
nb_long /= 10;
nb_size--;
}
return (nb_str);
}