-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
42 lines (39 loc) · 1.33 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: telias-p <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/13 18:28:43 by telias-p #+# #+# */
/* Updated: 2021/04/05 20:25:21 by telias-p ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa(int n)
{
char *str;
long nbr;
size_t size;
nbr = n;
size = n > 0 ? 0 : 1;
nbr = nbr > 0 ? nbr : -nbr;
while (n)
{
n /= 10;
size++;
}
if (!(str = (char *)malloc(size + 1)))
return (0);
*(str + size--) = '\0';
while (nbr > 0)
{
*(str + size--) = nbr % 10 + '0';
nbr /= 10;
}
if (size == 0 && str[1] == '\0')
*(str + size) = '0';
else if (size == 0 && str[1] != '\0')
*(str + size) = '-';
return (str);
}