-
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.37 KB
/
ft_itoa.c
File metadata and controls
59 lines (54 loc) · 1.37 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: falarm <falarm@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/16 16:55:32 by falarm #+# #+# */
/* Updated: 2021/10/16 20:25:12 by falarm ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
static size_t ft_intlen(int x)
{
size_t len;
len = 0;
if (x <= 0)
{
x = -x;
len = 1;
}
while (x)
{
x /= 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
char *s;
size_t len;
long int x;
x = n;
len = ft_intlen(x);
s = malloc(sizeof(char) * (len + 1));
if (!s)
return (NULL);
if (x == 0)
s[0] = '0';
if (x < 0)
{
s[0] = '-';
x = -x;
}
s[len--] = '\0';
while (x)
{
s[len--] = (x % 10) + '0';
x /= 10;
}
return (s);
}