-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_functions2.c
193 lines (183 loc) · 2.86 KB
/
print_functions2.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "holberton.h"
/**
* print_u - print unsigned integers
* @x: integer to print
* Return: number of byte printed
**/
int print_u(va_list x)
{
unsigned int n = va_arg(x, int), i = 1, divi = 1, j;
unsigned int m = n;
unsigned int bck = n;
if (n < 1)
{
_putchar('0' + 0);
return (1);
}
if (n >= 1 && n <= 9)
_putchar('0' + n);
else
{
while (m >= 10)
{
m = m / 10;
i++;
}
for (j = i; j > 1; j--)
divi = divi * 10;
for (j = 1; j <= i; j++)
{
m = bck / divi;
bck = bck - (m * divi);
divi = divi / 10;
_putchar('0' + m);
}
}
return (i);
}
/**
* print_oct - print number to octal
* @n: number to print
* Return: number of bytes printed
**/
int print_oct(va_list n)
{
unsigned int x = va_arg(n, int);
int res, i, j, count = 0;
char *ptr;
unsigned int y = x;
if (x < 1)
{
_putchar('0' + 0);
return (1);
}
for (j = 0; y > 0; j++)
y = y / 8;
ptr = malloc(sizeof(char) * j);
if (ptr == NULL)
return (-1);
for (i = 0; x > 0; i++)
{
res = x % 8;
x = x / 8;
ptr[i] = res;
count++;
}
for (; i > 0; i--)
{
_putchar('0' + ptr[i - 1]);
}
free(ptr);
return (count);
}
/**
* print_hex - print number to hexa lower
* @n: number to print
* Return: number of bytes printed
**/
int print_hex(va_list n)
{
unsigned int x = va_arg(n, int);
int res, i, j, count = 0;
char *ptr;
unsigned int y = x;
if (x < 1)
{
_putchar('0' + 0);
return (1);
}
for (j = 0; y > 0; j++)
y = y / 16;
ptr = malloc(sizeof(char) * j);
if (ptr == NULL)
return (-1);
for (i = 0; x > 0; i++)
{
res = x % 16;
x = x / 16;
if (res > 9 && res < 16)
ptr[i] = 39 + res;
else
ptr[i] = res;
count++;
}
for (; i > 0; i--)
{
_putchar('0' + ptr[i - 1]);
}
free(ptr);
return (count);
}
/**
* print_HEX - print number to hexa upper
* @n: number to print
* Return: number of bytes printed
**/
int print_HEX(va_list n)
{
unsigned int x = va_arg(n, int);
int res, i, j, count = 0;
char *ptr;
unsigned int y = x;
if (x < 1)
{
_putchar('0' + 0);
return (1);
}
for (j = 0; y > 0; j++)
y = y / 16;
ptr = malloc(sizeof(char) * j);
if (ptr == NULL)
return (-1);
for (i = 0; x > 0; i++)
{
res = x % 16;
x = x / 16;
if (res > 9 && res < 16)
ptr[i] = 7 + res;
else
ptr[i] = res;
count++;
}
for (; i > 0; i--)
{
_putchar('0' + ptr[i - 1]);
}
free(ptr);
return (count);
}
/**
* print_S - print non printable characters
* @s: string
* Return: bytes printed
**/
int print_S(va_list s)
{
unsigned int i, count = 0, aux;
char *str;
str = va_arg(s, char*);
if (str == NULL)
str = "(null)";
for (i = 0; str[i]; i++)
{
if (str[i] < 32 || str[i] >= 127)
{
aux = str[i];
_putchar('\\');
_putchar('x');
count = count + 2;
if (aux > 0 && aux < 16)
{
_putchar('0');
count++;
}
count = count + print_X(aux);
}
else
{
_putchar(str[i]);
count++;
}
}
return (count);
}