-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintf.c
62 lines (59 loc) · 1.13 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
#include <stdarg.h>
#include <stddef.h>
#include "main.h"
print_type print_format[] = {
{'c', print_char},
{'s', print_string},
{'i', print_int},
{'d', print_int},
{'%', print_percent},
{'\0', NULL}
};
/**
* _printf - produces output according to a format.
* @format: string to be printed
*
* Return: length of the string i.e. format
*/
int _printf(const char *format, ...)
{
int i, j, len, printed;
va_list arg;
if (format == NULL)
return (-1);
va_start(arg, format);
for (i = len = 0; format[i] != '\0'; i++)
{
if (format[i] != '%')
{
_putchar(format[i]);
len++; }
else if (format[i] == '%')
{
for (j = 0; print_format[j].type != '\0'; j++)
{
if (format[i + 1] == '\0')
{
va_end(arg);
return (-1);
}
else if (print_format[j].type == format[i + 1])
{
printed = print_format[j].f(arg);
len = len + printed;
i++;
break;
}
else if (format[i + 1] != 'c' && format[i + 1] != 's' &&
format[i + 1] != 'd' && format[i + 1] != 'i' && format[i + 1] != '%')
{
_putchar('%');
len++;
break;
}
}
}
}
va_end(arg);
return (len);
}