-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
56 lines (51 loc) · 1.42 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbetz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/07 09:39:00 by rbetz #+# #+# */
/* Updated: 2022/04/07 11:21:47 by rbetz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count(int c)
{
int n;
n = 0;
if (c == 0)
return (1);
while (c != 0)
{
c = c / 10;
n++;
}
return (n);
}
char *ft_itoa(int nbr)
{
char *str;
int i;
int sign;
i = ft_count(nbr);
sign = 0;
if (nbr < 0)
sign = 1;
str = malloc(i + sign + 1);
if (str == NULL)
return (NULL);
str[i + sign] = '\0';
if (sign == 1)
str[0] = '-';
while (i > 0)
{
if (sign == 1)
str[i + sign - 1] = (nbr % 10) * -1 + '0';
else
str[i + sign - 1] = nbr % 10 + '0';
nbr = nbr / 10;
i--;
}
return (str);
}