-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
56 lines (51 loc) · 1.46 KB
/
Copy pathft_itoa.c
File metadata and controls
56 lines (51 loc) · 1.46 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ophuong <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/13 21:39:49 by ophuong #+# #+# */
/* Updated: 2019/09/18 18:21:36 by ophuong ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int f_lenum(int n)
{
int i;
i = 0;
if (n == 0)
return (i + 1);
while (n != 0)
{
i++;
n = n / 10;
}
return (i);
}
char *ft_itoa(int n)
{
int len;
char *fresh;
unsigned int nb;
nb = n;
len = f_lenum(n);
if (n < 0)
{
if (!(fresh = ft_strnew(len + 1)))
return (NULL);
fresh[0] = '-';
len = len + 1;
nb = -n;
}
else if (!(fresh = ft_strnew(len)))
return (NULL);
fresh[len] = '\0';
while ((len != 0 && fresh[0] != '-') || (fresh[0] == '-' && nb != 0))
{
len--;
fresh[len] = (nb % 10) + '0';
nb = nb / 10;
}
return (fresh);
}