-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
42 lines (39 loc) · 1.27 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: kypark <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/02 15:32:31 by kypark #+# #+# */
/* Updated: 2020/11/04 17:40:31 by kypark ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa(int n)
{
char *s;
long long m;
long long i;
m = n;
i = 1;
while (m /= 10)
i++;
if (n < 0)
i++;
if (n == -2147483648)
return (s = ft_strdup("-2147483648"));
if (!(s = malloc(sizeof(char) * (i + 1))))
return (NULL);
if ((m = n) < 0)
m = -n;
s[i] = '\0';
while (--i >= 0)
{
s[i] = (m % 10) + '0';
m /= 10;
}
if (n < 0)
s[0] = '-';
return (s);
}