-
Notifications
You must be signed in to change notification settings - Fork 0
/
_printf.c
48 lines (43 loc) · 994 Bytes
/
_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
#include "main.h"
/**
* _printf -it is a fuction that selects the correct function to print
* @format:it is an identifier to look for
* Return:it will return the length of the strig.
*/
int _printf(const char * const format, ...)
{
convert p[] = {
{"%s", print_s}, {"%c", print_c},
{"%%", print_37},
{"%i", print_i}, {"%d", print_d}, {"%r", print_revs},
{"%R", print_rot13}, {"%b", print_bin},
{"%u", print_unsigned},
{"%o", print_oct}, {"%x", print_hex}, {"%X", print_HEX},
{"%S", print_exc_string}, {"%p", print_pointer}
};
va_list args;
int i = 0, j, length = 0;
va_start(args, format);
if (format == NULL || (format[0] == '%' && format[1] == '\0'))
return (-1);
Here:
while (format[i] != '\0')
{
j = 13;
while (j >= 0)
{
if (p[j].ph[0] == format[i] && p[j].ph[1] == format[i + 1])
{
length += p[j].function(args);
i = i + 2;
goto Here;
}
j--;
}
_putchar(format[i]);
length++;
i++;
}
va_end(args);
return (length);
}