-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
42 lines (39 loc) · 1.25 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkwayiba <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/21 09:25:26 by jkwayiba #+# #+# */
/* Updated: 2019/06/21 09:26:59 by jkwayiba ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa(int n)
{
char *str;
int len;
long nb;
nb = n;
len = ft_nbrlen(nb);
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL);
str[len--] = '\0';
if (n == 0)
{
str[0] = 48;
return (str);
}
if (n < 0)
{
str[0] = '-';
nb *= -1;
}
while (nb > 0)
{
str[len--] = 48 + (nb % 10);
nb = nb / 10;
}
return (str);
}