-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
43 lines (38 loc) · 1.3 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amatshiy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/06/11 11:50:27 by amatshiy #+# #+# */
/* Updated: 2017/06/11 11:51:27 by amatshiy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_intlen(int n)
{
size_t i;
i = 1;
if (n < 0)
i++;
while (n /= 10)
i++;
return (i);
}
char *ft_itoa(int n)
{
size_t len;
char *str;
unsigned int u_nbr;
len = ft_intlen(n);
u_nbr = (n < 0) ? -n : n;
if (!(str = ft_strnew(len)))
return (NULL);
str[--len] = u_nbr % 10 + '0';
while (u_nbr /= 10)
str[--len] = u_nbr % 10 + '0';
if (n < 0)
str[0] = '-';
return (str);
}