-
Notifications
You must be signed in to change notification settings - Fork 3
/
_printf.c
51 lines (48 loc) · 825 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
48
49
50
51
#include <stdarg.h>
#include "holberton.h"
/**
*_printf - prints to output according to format
*@format: character string
*
*Return: number of characters printed
*/
int _printf(const char *format, ...)
{
int i = 0, j = 0;
int (*f)(va_list);
va_list args;
va_start(args, format);
if (format == NULL || !format[i + 1])
return (-1);
while (format[i])
{
if (format[i] == '%')
{
if (format[i + 1])
{
if (format[i + 1] != 'c' && format[i + 1] != 's'
&& format[i + 1] != '%' && format[i + 1] != 'd'
&& format[i + 1] != 'i')
{
j += _putchar(format[i]);
j += _putchar(format[i + 1]);
i++;
}
else
{
f = get_func(&format[i + 1]);
j += f(args);
i++;
}
}
}
else
{
_putchar(format[i]);
j++;
}
i++;
}
va_end(args);
return (j);
}