-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
58 lines (53 loc) · 1.47 KB
/
Copy pathft_itoa.c
File metadata and controls
58 lines (53 loc) · 1.47 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gfoote <gfoote@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/05 16:41:53 by gfoote #+# #+# */
/* Updated: 2019/04/05 21:56:43 by gfoote ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_base(int n)
{
int base;
base = 0;
if (n < 0)
n *= -1;
if (n == 0)
base++;
while (n > 0)
{
base++;
n = n / 10;
}
return (base);
}
char *ft_itoa(int n)
{
char *result;
int base;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
base = ft_base(n);
result = ((n < 0) ? ft_strnew(base + 1) : ft_strnew(base));
if (!result)
return (NULL);
if (n < 0)
{
result[0] = '-';
n *= -1;
}
while (base > 0)
{
if (result[0] != '-')
base--;
result[base] = n % 10 + '0';
n = n / 10;
if (result[0] == '-')
base--;
}
return (result);
}