-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintf.c
More file actions
executable file
·90 lines (60 loc) · 1.4 KB
/
printf.c
File metadata and controls
executable file
·90 lines (60 loc) · 1.4 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
83
84
85
86
87
88
89
90
#include "main.h"
/**
* _printf - selects the correct function to print based on the format.
* @format: format specifier.
* Return: the length of the printed string.
*/
ConversionSpecifier m[] = {
{"%s", printf_string}, {"%c", printf_char},
{"%i", printf_int}, {"%d", printf_dec}, {"%r", \
printf_strrev},
{"%R", printf_ROT13}, {"%b", printf_bin}, {"%u"\
, printf_unsigned},
{"%o", printf_oct}, {"%x", printf_hex}, {"%p", \
printf_pointer}
};
int _printf(const char *format, ...) {
int len =0;
int match_found = 0;
int j = 0;
int m_len;
va_list pars;
if (format == NULL || format[0] == '\0')
return -1;
va_start(pars, format);
while (*format) {
if (*format == '%') {
format++;
m_len = sizeof(m) / sizeof(m[0]);
for (j = 0; j < m_len; j++) {
}
{
if (format[0] == m[j].id[0] && format[1] == m[j].id[1]) {
len += m[j].f(pars);
format += 2;
match_found = 1;
break;
}
}
if (!match_found) {
_putchar('%');
len++;
}
} else {
_putchar(*format);
len++;
}
format++;
}
va_end(pars);
return len;
}
/**
* Add the conversion specifier structure
int handle_percent_r(va_list pars) {
* Implement the behavior for %r (e.g., print it as is)
putchar('%');
putchar('r');
return 2;
}
*/