-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
83 lines (76 loc) · 1.78 KB
/
ft_itoa.c
File metadata and controls
83 lines (76 loc) · 1.78 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
79
80
81
82
83
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pruthann <pruth@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/14 15:55:48 by pruthann #+# #+# */
/* Updated: 2020/11/14 15:56:27 by pruthann ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void ft_putnbr_str(char *str, int n, int i)
{
char sign;
if (n >= 10 || n <= -10)
{
if (n <= -10)
sign = -(n % 10) + 48;
else
sign = n % 10 + 48;
ft_putnbr_str(str, n / 10, i + 1);
str[i] = sign;
}
else if (n < 0)
{
sign = -n + 48;
str[i] = sign;
str[i + 1] = '-';
}
else
{
sign = n + 48;
str[i] = sign;
}
}
static int ft_intlen(int n)
{
int len;
len = 1;
if (n < 0)
len++;
while (n >= 10 || n <= -10)
{
len++;
n = n / 10;
}
return (len);
}
char *ft_itoa(int n)
{
int i;
int len;
char *str;
char temp;
int temp_len;
i = 0;
len = ft_intlen(n);
str = malloc(sizeof(char) * (len + 1));
if (str == 0)
return (0);
ft_putnbr_str(str, n, 0);
temp_len = len;
len = len / 2;
while (len)
{
temp = str[temp_len - 1];
str[temp_len - 1] = str[i];
str[i] = temp;
temp_len--;
len--;
i++;
}
str[ft_intlen(n)] = '\0';
return (str);
}