-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
59 lines (54 loc) · 1.42 KB
/
ft_itoa.c
File metadata and controls
59 lines (54 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
56
57
58
59
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tparand <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/10 16:41:48 by tparand #+# #+# */
/* Updated: 2017/11/21 12:47:08 by tparand ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t get_length(long int *nb)
{
size_t size;
long int n;
size = 0;
n = *nb;
if (n == 0)
return (1);
if (n < 0)
{
*nb *= -1;
n *= -1;
size++;
}
while (n > 0)
{
n /= 10;
size++;
}
return (size);
}
char *ft_itoa(int n)
{
char *str;
size_t str_len;
long int nb;
nb = (long int)n;
str_len = get_length(&nb);
str = ft_strnew(str_len);
if (!str)
return (NULL);
*str = '0';
if (n < 0)
*str = '-';
while (nb > 0)
{
str[str_len - 1] = nb % 10 + '0';
nb /= 10;
str_len--;
}
return (str);
}