-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
41 lines (38 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dbaldy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/28 12:37:49 by dbaldy #+# #+# */
/* Updated: 2015/11/30 12:14:54 by dbaldy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa(int n)
{
char *str;
int negative;
int size;
int buf;
negative = (n < 0);
size = 1;
buf = n;
while (buf /= 10)
size++;
str = ft_strnew(size + negative);
if (str == NULL)
return (NULL);
if (n == -2147483648)
return (ft_strdup("-2147483648"));
str[0] = '-';
n = (negative) ? -n : n;
str[size + negative] = '\0';
while (size--)
{
str[size + negative] = n % 10 + '0';
n /= 10;
}
return (str);
}