-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
65 lines (61 loc) · 1.23 KB
/
_printf.c
File metadata and controls
65 lines (61 loc) · 1.23 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
#include "main.h"
#include <stdio.h>
int ip(int count);
/**
* _printf - prints output according to a format
* @format: format string to print
* Return: int number of characters printed
*/
int _printf(const char *format, ...)
{
int i, j, count = 0;
va_list arg;
char buffer[2048] = {'\0'}, *(*f)(va_list), *newStr;
if (format == NULL)
return (-1);
va_start(arg, format);
for (i = 0, j = 0; format[i] != '\0'; i++, j++)
{
if (format[i] == '%' && format[i + 1] == '\0')
return (-1);
if (format[i] == '%')
{
if (format[i + 1] == '%')
buffer[j] = format[i];
else
{
f = get_func(format[i + 1]);
if (f == NULL)
{
buffer[j] = format[i];
buffer[++j] = format[i + 1];
}
else
{
newStr = f(arg);
_strcat(buffer, newStr);
j += _strlen(newStr) - 1;
if (newStr[0] == '\0' && newStr[1] == '\0')
count = ip(count);
}
}
i++;
}
else
buffer[j] = format[i];
}
va_end(arg);
return (count + cpstr(buffer));
}
/**
* ip - increment variable and print null character
* @count: var to increment
* Return: incremented variable
*
* Description: purpose - to satisfy the betty linter's <40 line req.
*/
int ip(int count)
{
_putchar('\0');
return (count + 1);
}