-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
78 lines (72 loc) · 1.82 KB
/
ft_itoa.c
File metadata and controls
78 lines (72 loc) · 1.82 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simarcha <simarcha@student.42barcel> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 19:39:31 by simarcha #+# #+# */
/* Updated: 2024/01/20 20:55:10 by simarcha ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_ctr(long num)
{
size_t ctr;
ctr = 0;
while (num < 0)
{
num *= -1;
ctr++;
}
while (num > 0)
{
num /= 10;
ctr++;
}
return (ctr);
}
char *ft_itoa(int n)
{
char *str;
long tmp;
size_t len;
tmp = n;
len = ft_ctr(tmp);
if (n == 0)
return (ft_strdup("0"));
str = malloc((len + 1) * (sizeof(char)));
if (!str)
return (NULL);
str[len--] = '\0';
if (tmp < 0)
{
tmp *= -1;
str[0] = '-';
}
while (tmp > 0)
{
str[len--] = (tmp % 10) + '0';
tmp /= 10;
}
return (str);
}
/*
#include <stdio.h>
int main(void)
{
int n = +24;
char *res;
res = ft_itoa(n);
//int nn = 0;
//int nnn = -2147483648;
//int nnnn = 65543767;
//long m = -2147483649;
printf("Num %d: %s\n", n, ft_itoa(n));
//printf("Num %d: %s\n", nn, ft_itoa(nn));
//printf("Num %d: %s\n", nnn, ft_itoa(nnn));
//printf("Num %d: %s\n", nnnn, ft_itoa(nnnn));
//printf("Num %ld: %s\n", m, ft_itoa(m));
printf("%s\n", res);
return (0);
}*/