-
Notifications
You must be signed in to change notification settings - Fork 1
/
_printf.c
45 lines (44 loc) · 1 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
#include "main.h"
#include <stdarg.h>
#include <stdlib.h>
/**
* _printf - print string
* @format: format
* Return: letter count
*/
int _printf(const char *format, ...)
{
va_list ptr;
unsigned int j = 0, r = 0;
if (format == NULL)
exit(98);
va_start(ptr, format);
for (j = 0; *(format + j) != '\0'; j++)
{
if (*(format + j) != '%')
{
r++, _putchar(*(format + j)); }
else if (*(format + j) == '%' && *(format + j + 1) == '\0')
continue;
else if (*(format + j) == '%' && *(format + j + 1) == '%')
{
r++, j++, _putchar('%'); }
else if (*(format + j) == '%' && *(format + j + 1) == 'c')
{
r = print_char(r, va_arg(ptr, int));
j++; }
else if (*(format + j) == '%' && *(format + j + 1) == 's')
{
r = print_string(r, va_arg(ptr, char *));
j++; }
else if ((*(format + j) == '%') && ((*(format + j + 1) == 'd')
|| (*(format + j + 1) == 'i')))
{
r = print_decimal(r, va_arg(ptr, int));
j++; }
else
r++, _putchar(*(format + j));
}
if (r == 0)
r = -1;
return (r); }