-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
46 lines (41 loc) · 1.33 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ychahbar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/07/02 14:43:43 by ychahbar #+# #+# */
/* Updated: 2018/07/06 14:14:42 by ychahbar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t get_str_len(int n)
{
size_t i;
i = 1;
while (n /= 10)
i++;
return (i);
}
char *ft_itoa(int n)
{
char *str;
size_t str_len;
unsigned int n_cpy;
str_len = get_str_len(n);
n_cpy = n;
if (n < 0)
{
n_cpy = -n;
str_len++;
}
if (!(str = ft_strnew(str_len)))
return (NULL);
str[--str_len] = n_cpy % 10 + '0';
while (n_cpy /= 10)
str[--str_len] = n_cpy % 10 + '0';
if (n < 0)
*(str + 0) = '-';
return (str);
}