-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_printf.c
51 lines (50 loc) · 1.12 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
#include "main.h"
/**
*_printf - function that produces output according to a format.
*@format: the format string
*Return: number of characters printed (excluding the null)
*/
int _printf(const char *format, ...)
{
va_list args;
int counter, num, iter;
char *str;
counter = 0, iter = 0;
va_start(args, format);
if (!format || (format[0] == '%' && !format[1]))
return (-1);
if (format[0] == '%' && format[1] == ' ' && !format[2])
return (-1);
while (format[iter] != '\0')
{
if (format[iter] == '%')
{
if (format[iter + 1] == 'c')
{
num = va_arg(args, int);
counter += _putchar(num);
}
else if (format[iter + 1] == 's')
{
str = va_arg(args, char *);
counter += _strlen(str);
_puts_recursion(str);
}
else if (format[iter + 1] == '%')
counter += _putchar('%');
else if ((format[iter + 1] == 'i') || (format[iter + 1] == 'd'))
{
num = va_arg(args, int);
counter += print_number(num);
}
else
counter += _putchar(format[iter + 1]);
iter += 2;
}
_putchar(format[iter]);
counter++;
iter++;
}
va_end(args);
return ((counter == 0) ? -1 : counter);
}