-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprintf.c
More file actions
82 lines (76 loc) · 1.43 KB
/
printf.c
File metadata and controls
82 lines (76 loc) · 1.43 KB
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
#include <stdarg.h>
#include "holberton.h"
#include <stdlib.h>
/**
* selection - Function that selects the correct function to print.
* @format: Address to String.
* @list1: A Valist.
* @ops1: Address to an Structure.
*
* Return: an int.
*/
int selection(const char *format, va_list list1, functions *ops1)
{
int i = 0, j = 0, count = 0;
while (format[i] && format)
{
if (format[i] == '%' && format[i + 1] == '\0')
return (-1);
if (format[i] == '%' && (format[i + 1] == ' ' || format[i + 1] != '%'))
{
if (format[i + 1] == ' ')
{
while (format[i + 1] == ' ')
i++;
}
while (j < 6)
{
if (ops1[j].opc[0] == format[i + 1])
{
count = count + ops1[j].f(list1);
i++;
break;
}
j++;
}
if (j == 6)
count = count + _putchar('%');
}
else if (format[i] == '%' && format[i + 1] == '%')
{
count = count + _putchar('%');
i++;
}
else
{
count = count + _putchar(format[i]);
}
i++;
j = 0;
}
return (count);
}
/**
* _printf - Function that produces output according to a format.
* @format: Address to String.
* Return: An int.
*/
int _printf(const char *format, ...)
{
va_list list;
int i = 0;
functions ops[] = {
{"c", p_char},
{"s", p_string},
{"i", p_integer},
{"d", p_integer},
{"r", p_reverse},
{"R", p_rot13},
};
if (format == NULL)
return (-1);
va_start(list, format);
i = selection(format, list, ops);
va_end(list);
return (i);
}