-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_printf.c
More file actions
134 lines (115 loc) · 2.08 KB
/
_printf.c
File metadata and controls
134 lines (115 loc) · 2.08 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "main.h"
/**
* _strlen - Returns the length of a string.
*
* @string: Pointer to a first letter of a string.
*
* Return: The length of an string
*/
int _strlen(const char *string)
{
int size = 0;
if (string)
{
while (string[size] != '\0')
size++;
return (size);
}
write(1, "You must to enter an string (NOT NULL)", 39);
return (-1);
}
/**
* _strcmp - Compare a string with another
*
* @s1: string.
*
* @s2: string.
*
* Return: Void
*/
int _strcmp(char *s1, char *s2)
{
int i;
int result = 0;
for (i = 0; s1[i] != '\0' && s2[i] != '\0'; i++)
{
if (s1[i] != s2[i])
{
result = s1[i] - s2[i];
break;
}
}
return (result);
}
/**
* choose_option - Find if the flag match with an existence and
* give the correct function.
*
* @s: Character to pass
*
* Return: A print_operation struct with a flag (0 or 1
* and a function pointer or NULL.
*/
print_operation_t choose_option(char s)
{
print_operation_t test = {0, NULL};
special_chars_t options[] = {
{'d', print_num},
{'c', print_char},
{'s', print_str},
{'i', print_int},
{'%', print_percent},
{'b', print_binary},
{'R', print_rot13},
{'r', print_str_rev},
{'\0', NULL}
};
int i = 0;
while (options[i].op)
{
if (options[i].op == s)
{
test.flag = 1;
test.print = options[i].operation;
return (test);
}
i++;
}
return (test);
}
/**
* _printf - Produce output according to a format as described below.
*
* @format: A string with the format wished.
*
* Return: The length of the final string with the format
*/
int _printf(const char *format, ...)
{
int i = 0, counter = 0, plus = 0;
print_operation_t p = {0, NULL};
va_list args;
if (!format || (format[0] == '%' && format[1] == '\0'))
return (-1);
if (format[0] == '\0')
return (0);
va_start(args, format);
for (i = 0; format[i] != '\0'; i++)
{
if (format[i] == '%')
{
p = choose_option(format[i + 1]);
if (p.flag == 1)
{
plus = p.print(args);
counter += plus + 1;
i += 1;
continue;
}
}
_putchar(format[i]);
counter++;
}
va_end(args);
return (counter);
}