-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
66 lines (61 loc) · 1.51 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: fgata-va <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/21 17:07:18 by fgata-va #+# #+# */
/* Updated: 2021/03/04 19:17:40 by fgata-va ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *ft_revstr(char *str)
{
char tmp[13];
int i;
int j;
i = 0;
j = ft_strlen(str) - 1;
if (str[i] == '-')
{
tmp[i] = '-';
i++;
}
while (str[i] != '\0')
{
tmp[i] = str[j];
i++;
j--;
}
tmp[i] = '\0';
str = ft_memcpy(str, tmp, ft_strlen(tmp));
return (str);
}
char *ft_itoa(int n)
{
char tmp[12];
char *rtmp;
char *str;
int i;
long int nb;
nb = n;
i = 0;
if (nb < 0)
{
tmp[i++] = '-';
nb *= -1;
}
if (nb == 0)
tmp[i++] = '0';
while (nb > 0)
{
tmp[i] = ((nb % 10) + '0');
nb /= 10;
i++;
}
tmp[i] = '\0';
rtmp = ft_revstr(tmp);
str = ft_strdup(rtmp);
return (str);
}