-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
55 lines (50 loc) · 1.42 KB
/
ft_itoa.c
File metadata and controls
55 lines (50 loc) · 1.42 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jamrabhi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/23 20:57:41 by jamrabhi #+# #+# */
/* Updated: 2021/06/05 19:39:10 by jamrabhi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_intlen(int n)
{
size_t len;
len = 1;
if (n < 0)
len++;
while (n >= 10 || n <= -10)
{
n /= 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
char *rt;
unsigned int nb_cpy;
size_t len;
nb_cpy = n;
len = ft_intlen(n);
rt = (char *)malloc(sizeof(*rt) * (len + 1));
if (!rt)
return (NULL);
rt[len--] = '\0';
if (n < 0)
{
nb_cpy = n * -1;
rt[0] = '-';
}
if (nb_cpy == 0)
rt[len] = 0 + '0';
while (nb_cpy != 0)
{
rt[len--] = (nb_cpy % 10 + '0');
nb_cpy = nb_cpy / 10;
}
return (rt);
}