-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
56 lines (51 loc) · 1.48 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
56
/* ************************************************************************** */
/* LE - / */
/* / */
/* ft_itoa.c .:: .:/ . .:: */
/* +:+:+ +: +: +:+:+ */
/* By: davfelix <[email protected]> +:+ +: +: +:+ */
/* #+# #+ #+ #+# */
/* Created: 2018/10/26 14:50:32 by davfelix #+# ## ## #+# */
/* Updated: 2018/10/31 11:58:46 by davfelix ### #+. /#+ ###.fr */
/* / */
/* / */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_len(unsigned int nb, int n)
{
size_t len;
len = 0;
if (n <= 0)
len++;
while (nb > 0)
{
nb = nb / 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
size_t len;
char *str;
unsigned int nb;
if (n < 0)
nb = -n;
else
nb = n;
len = ft_len(nb, n);
str = ft_strnew(len);
if (str == NULL)
return (NULL);
if (n == 0)
str[0] = '0';
else if (n < 0)
str[0] = '-';
while (len > 0 && nb > 0)
{
str[len - 1] = (nb % 10 + '0');
nb = nb / 10;
len--;
}
return (str);
}