forked from wrightkhlebisol/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
printf.c
351 lines (309 loc) · 5.14 KB
/
printf.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include "main.h"
#include <limits.h>
#include <stdlib.h>
int _printf(const char *format, ...);
/**
* print_null - Print null as string
*/
void print_null(void)
{
char *nullstr = "(null)";
while (*nullstr != '\0')
{
_putchar(*nullstr);
nullstr++;
}
}
/**
* printstring - print a string
* @str: String to print
* Return: Number of characters printed
*/
int printstring(char *str)
{
int count = 0, nullLength = 6;
if (str == NULL)
{
print_null();
return (nullLength);
}
while (*str != '\0')
{
_putchar(*str);
str++;
count++;
}
return (count);
}
/**
* countchar - count number of chars
* @str: string to count
* Return: Length of char
*/
int countchar(char *str)
{
int charcount = 0;
while (*str != '\0')
{
charcount++;
str++;
}
return (charcount);
}
/**
* printreverse - Print reversed string
* @str: String to reverse
* Return: length of written string
*/
int printreverse(char *str)
{
int count = 0, nullLength = 6, charcount = 0;
if (str == NULL)
{
print_null();
return (nullLength);
}
charcount = countchar(str);
while (charcount != 0)
{
count++;
_putchar(str[--charcount]);
}
return (count);
}
/**
* print_num - print all digit of numbers
* @num: number to be printed
* Return: number of characters printed
*/
int print_num(long int num)
{
long int rev = 0, digit, count = 0;
if (num == INT_MIN)
{
_putchar('-');
_putchar('2');
num = 147483648;
count += 2;
}
if (num == INT_MAX)
{
_putchar('2');
num = 147483647;
count += 1;
}
if (num < 0)
{
_putchar('-');
num = -num;
count += 1;
}
if (num == 0)
{
_putchar('0');
count += 1;
return (count);
}
while (num > 0)
{
int digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
count += 1;
}
while (rev > 0)
{
digit = rev % 10;
_putchar(digit + '0');
rev /= 10;
}
return (count);
}
/**
* print_bin - converts interger to binary
* @num: number to be converted
* @count: parameter
* Return: number of characters printed
*/
int print_bin(unsigned int num, int *count)
{
if (num > 0)
{
print_bin(num / 2, count);
_putchar((num % 2) + '0');
(*count)++;
}
return (0);
}
/**
* print_unsigned_int - print unsigned int
* @num: number to be printed
* @count: parameter
* Return: 0
*/
int print_unsigned_int(unsigned int num, int *count)
{
if (num >= 10)
{
print_unsigned_int(num / 10, count);
}
_putchar((num % 10) + '0');
(*count)++;
return (0);
}
/**
* print_octal - converts interger to binary
* @num: number to be converted
* @count: parameter
* Return: number of characters printed
*/
int print_octal(unsigned int num, int *count)
{
if (num > 0)
{
print_octal(num / 8, count);
_putchar((num % 8) + '0');
(*count)++;
}
return (0);
}
/**
* print_hexa_x - converts interger to hexadecimal
* @num: number to be converted
* @count: parameter
* Return: number of characters printed
*/
int print_hexa_x(unsigned int num, int *count)
{
int rem;
int count_x = 0;
if (num >= 16)
{
print_hexa_x((num / 16), count);
}
rem = num % 16;
if (rem < 10)
{
_putchar(rem + '0');
count_x++;
}
else
{
_putchar('a' + rem - 10);
count_x++;
}
*count += count_x;
return (count_x);
}
/**
* print_hexa_X - converts interger to hexadecimal
* @num: number to be converted
* @count: parameter
* Return: number of characters printed
*/
int print_hexa_X(unsigned int num, int *count)
{
int rem;
int count_X = 0;
if (num >= 16)
{
print_hexa_X(num / 16, count);
}
rem = num % 16;
if (rem < 10)
{
_putchar(rem + '0');
count_X++;
}
else
{
_putchar('A' + rem - 10);
count_X++;
}
*count += count_X;
return (count_X);
}
/**
* _printf - Print Formatted output
* @format: Formatted for given string
* Return: Int
*/
int _printf(const char *format, ...)
{
va_list ap;
int count = 0;
if (format == NULL)
exit(139);
va_start(ap, format);
while (*format != '\0')
{
if (*format == '%')
{
if (*++format == 'c')
{
_putchar((unsigned char)va_arg(ap, int));
count++;
}
else if (*format == 's')
{
char *str = va_arg(ap, char *);
count += printstring(str);
}
else if (*format == 'r')
{
char *str = va_arg(ap, char *);
count += printreverse(str);
}
else if (*format == 'd' || *format == 'i')
{
int num = va_arg(ap, long int);
count += print_num(num);
}
else if (*format == 'b')
{
int num = va_arg(ap, unsigned int);
count += print_bin(num, &count);
}
else if (*format == 'u')
{
unsigned int num = va_arg(ap, unsigned int);
count += print_unsigned_int(num, &count);
}
else if (*format == 'o')
{
unsigned int num = va_arg(ap, unsigned int);
count += print_octal(num, &count);
}
else if (*format == 'x')
{
int num = va_arg(ap, unsigned int);
count += print_hexa_x(num, &count);
count--;
}
else if (*format == 'X')
{
int num = va_arg(ap, unsigned int);
count += print_hexa_X(num, &count);
count--;
}
else if (*format == '%')
{
count++;
_putchar(*format);
}
else
{
count++;
_putchar(*--format);
}
}
else
{
_putchar(*format);
count++;
}
format++;
}
va_end(ap);
return (count);
}