-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
36 lines (33 loc) · 1.23 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ylagtab <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/03 11:45:26 by ylagtab #+# #+# */
/* Updated: 2020/10/15 09:34:20 by ylagtab ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa(int n)
{
unsigned int nb;
char *res;
size_t size;
int index;
nb = n > 0 ? n : -n;
size = ft_nbrlen(nb) + (n < 0);
if ((res = ft_strnew(size)) == NULL)
return (NULL);
if (n <= 0)
res[0] = n < 0 ? '-' : '0';
index = size - 1;
while (nb > 0)
{
res[index] = nb % 10 + '0';
nb /= 10;
index--;
}
return (res);
}