-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
59 lines (54 loc) · 1.54 KB
/
Copy pathft_itoa.c
File metadata and controls
59 lines (54 loc) · 1.54 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dramos-h <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/28 11:18:02 by dramos-h #+# #+# */
/* Updated: 2022/12/28 11:18:08 by dramos-h ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void itoa_isnegative(int *n, int *negative)
{
if (*n < 0)
{
*n *= -1;
*negative = 1;
}
}
char *ft_itoa(int n)
{
int aux;
int len;
int negative;
char *str;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
aux = n;
len = 1;
negative = 0;
itoa_isnegative(&n, &negative);
while (aux /= 10)
len++;
len += negative;
if ((str = (char *)malloc(sizeof(char) * len + 1)) == NULL)
return (NULL);
str[len] = '\0';
while (len--)
{
str[len] = n % 10 + '0';
n = n / 10;
}
if (negative)
str[0] = '-';
return (str);
}
/* int main ()
{
int x = -2147483648;
char *str = ft_itoa(x);
printf("%s", str);
return(0);
} */