-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
42 lines (39 loc) · 1.32 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lboukrou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/12/17 15:03:17 by lboukrou #+# #+# */
/* Updated: 2017/12/17 15:07:59 by lboukrou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa(int n)
{
size_t s_size;
long n_cp;
char *str;
n_cp = n;
s_size = ((n < 0) || !n) ? 1 : 0;
while (n_cp)
{
n_cp /= 10;
s_size++;
}
n_cp = n;
if ((str = malloc(sizeof(char) * (s_size + 1))) == NULL)
return (NULL);
if (!n)
str[0] = '0';
str[s_size] = 0;
while (n_cp)
{
str[s_size-- - 1] = n_cp < 0 ? (n_cp * -1) % 10 + '0' : n_cp % 10 + '0';
n_cp /= 10;
}
if (n < 0 && n)
str[0] = '-';
return (str);
}