-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
66 lines (59 loc) · 1.44 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajakob <ajakob@student.42heilbronn.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/21 17:48:44 by ajakob #+# #+# */
/* Updated: 2022/11/30 15:05:56 by ajakob ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int intlen(int n)
{
int i;
i = 1;
while (n / 10 != 0)
{
n = n / 10;
i++;
}
return (i);
}
static char *convtostr(long n, int len)
{
char *str;
int i;
i = 0;
str = malloc((len + 1) * sizeof(char));
if (!str)
return (NULL);
if (n < 0)
{
n *= -1;
str[i] = '-';
i = 1;
}
str[len] = '\0';
len--;
while (len >= i)
{
str[len] = n % 10 + 48;
n = n / 10;
len--;
}
return (str);
}
char *ft_itoa(int n)
{
char *str;
int len;
long i;
len = intlen(n);
if (n < 0)
len++;
i = n;
str = convtostr(i, len);
return (str);
}