-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
executable file
·61 lines (55 loc) · 1.49 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
59
60
61
/* ************************************************************************** */
/* LE - / */
/* / */
/* ft_itoa.c .:: .:/ . .:: */
/* +:+:+ +: +: +:+:+ */
/* By: brobicho <[email protected]> +:+ +: +: +:+ */
/* #+# #+ #+ #+# */
/* Created: 2017/12/04 03:19:54 by brobicho #+# ## ## #+# */
/* Updated: 2017/12/04 03:19:54 by brobicho ### #+. /#+ ###.fr */
/* / */
/* / */
/* ************************************************************************** */
#include "libft.h"
static int ft_pos(int n)
{
if (n < 0)
return (-n);
return (n);
}
static int ft_nblen(int n)
{
int len;
if (n <= 0)
len = 1;
else
len = 0;
while (n)
{
n = n / 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
int i;
int pos;
char *res;
if (n < 0)
pos = -1;
else
pos = 1;
i = ft_nblen(n);
if (!(res = (char*)malloc(sizeof(char) * i + 1)))
return (NULL);
res[i] = '\0';
while (--i >= 0)
{
res[i] = ft_pos(n % 10) + 48;
n = ft_pos(n / 10);
}
if (pos == -1)
res[0] = '-';
return (res);
}