-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
executable file
·58 lines (53 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
57
58
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kbensado <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/12/24 20:29:23 by kbensado #+# #+# */
/* Updated: 2015/12/28 12:18:14 by kbensado ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_len_num(int n)
{
size_t len;
if (0 == n)
return (1);
len = 0;
if (0 > n)
len = 1;
while (0 != n)
{
len++;
n /= 10;
}
return (len);
}
char *ft_itoa(int n)
{
size_t len;
char *result;
len = ft_len_num(n);
result = (char*)malloc(sizeof(char) * (len + 1));
if (NULL == result)
return (NULL);
result[len] = 0;
if ((-2147483647 - 1) == n)
return (ft_strcpy(result, "-2147483648"));
if (0 > n)
{
result[0] = '-';
n = -n;
}
if (0 == n)
result[0] = '0';
while (0 != n)
{
len--;
result[len] = (n % 10) + '0';
n /= 10;
}
return (result);
}