-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0-printf.c
51 lines (50 loc) · 977 Bytes
/
0-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 "main.h"
/**
* _printf - mimic the standard printf function
* @...: list of arguments passed to the function
* @format: pointer to string
*
* Return: length of the string
*/
int _printf(const char *format, ...)
{
va_list args;
int count = 0, i = 0, j = 0;
spec_t spec_list[] = {{'c', spec_char}, {'s', spec_str}, {'%', spec_percent},
{'d', spec_deci}, {'i', spec_deci}, {'\0', NULL}};
va_start(args, format);
if (format == NULL)
return (-1);
while (format != NULL && format[i] != '\0')
{
if (format[i] == '%')
{
if (format[i + 1] == '\0')
return (-1);
while (spec_list[j].c != '\0')
{
if (format[i + 1] == spec_list[j].c)
{
count += spec_list[j].spec(args);
break;
}
j++;
}
if (spec_list[j].c == '\0')
{
count += _putchar(format[i]);
count += _putchar(format[i + 1]);
}
j = 0;
i++;
}
else
{
_putchar(format[i]);
count++;
}
i++;
}
va_end(args);
return (count);
}