-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf_utils_2.c
100 lines (89 loc) · 1.94 KB
/
ft_printf_utils_2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_utils_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ptroger <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/07 13:38:36 by ptroger #+# #+# */
/* Updated: 2020/01/07 13:52:31 by ptroger ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static char *ft_strnew(size_t size)
{
char *str;
if (!(str = (char *)malloc(sizeof(char) * (size + 1))))
return (NULL);
str[size] = '\0';
return (str);
}
static int ft_intlen(unsigned int n)
{
int len;
if (n == 0)
return (1);
len = 0;
if (n < 0)
len++;
while (n != 0)
{
n /= 10;
len++;
}
return (len);
}
char *ft_unsigned_itoa(unsigned int n)
{
int i;
char *s;
if (!(s = ft_strnew(ft_intlen(n))))
return (NULL);
i = 0;
if (n == 0)
s[i++] = '0';
while (n > 0)
{
s[(ft_intlen(n)) + i - 1] = n % 10 + 48;
n /= 10;
}
return (s);
}
char *ft_to_upper(char *str)
{
int i;
i = 0;
while (str[i])
{
if (str[i] >= 'a' && str[i] <= 'z')
str[i] -= ('a' - 'A');
i++;
}
return (str);
}
char *ft_itoa(int n)
{
int i;
char *s;
if (!(s = ft_strnew(ft_intlen(n))))
return (NULL);
i = 0;
if (n < 0)
{
s[i++] = '-';
if (n == -2147483648)
{
s[i++] = '2';
n = -147483648;
}
n = -n;
}
if (n == 0)
s[i++] = '0';
while (n > 0)
{
s[(ft_intlen(n)) + i - 1] = n % 10 + 48;
n /= 10;
}
return (s);
}