-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_printf.c
90 lines (84 loc) · 1.88 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
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 - a function that produces output according to a format.
* Return: the number of characters printed
* @format: is a character string.
*/
int _printf(const char *format, ...)
{
int index, char_count = 0, len;
va_list args;
char *str;
va_start(args, format);
/*checks if pointers points to NULL*/
if (format == NULL)
{
return (-1);
}
/*loop of the arguments*/
for (index = 0; format[index]; index++)
{
/*checks if the current character is a format specifier %*/
if (format[index] == '%')
{
index++;
if (format[index] == 'c')
{
char_count += _putchar(va_arg(args, int));
}
else if (format[index] == 's')
{
char_count += _puts(va_arg(args, char *));
}
else if (format[index] == 'i' || format[index] == 'd')
{
char_count += _print_int(va_arg(args, int));
}
else if (format[index] == 'b')
{
print_binary(va_arg(args, unsigned int));
char_count += sizeof(unsigned int) * 8;
}
else if (format[index] == 'u')
{
char_count += _output_unsigned_integer(va_arg(args, unsigned int));
}
else if (format[index] == 'o')
{
char_count += _output_oct(va_arg(args, unsigned int));
}
else if (format[index] == 'x')
{
char_count += low_hex(va_arg(args, unsigned int));
}
else if (format[index] == 'X')
{
char_count += uppr_hex(va_arg(args, unsigned int));
}
else if (format[index] == 'p')
{
char_count += _output_pointer(va_arg(args, void *));
}
else if (format[index] == 'r')
{
str = va_arg(args, char *);
char_count += _output_rev(str);
}
else if (format[index] == 'R')
{
len = _strlen(args);
char_count += _output_rot13(va_arg(args, char *), len);
}
else if (format[index] == '%')
{
char_count += _putchar('%');
}
}
else
{
char_count += _putchar(format[index]);
}
}
va_end(args);
return (char_count);
}