-
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.48 KB
/
ft_itoa.c
File metadata and controls
59 lines (54 loc) · 1.48 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: mattig <mattig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/30 11:36:53 by mattig #+# #+# */
/* Updated: 2021/11/05 20:12:37 by mattig ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
static int count_lenght(long nb)
{
int lenght;
lenght = 0;
if (nb == 0)
return (1);
if (nb < 0)
{
nb = nb * -1;
lenght++;
}
while (nb > 0)
{
nb = nb / 10;
lenght++;
}
return (lenght);
}
char *ft_itoa(int n)
{
char *ptr_str;
int len;
int sign;
long nb;
sign = 1;
nb = (long)n;
len = count_lenght(nb);
ptr_str = (char *)malloc(sizeof(char) * len + 1);
if (ptr_str == NULL)
return (NULL);
ptr_str[len] = '\0';
if (nb < 0)
sign *= -1;
while (len--)
{
ptr_str[len] = sign * nb % 10 + 48;
nb /= 10;
}
if (sign < 0)
ptr_str[0] = '-';
return (ptr_str);
}