-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintf.c
More file actions
123 lines (111 loc) · 1.61 KB
/
Copy pathprintf.c
File metadata and controls
123 lines (111 loc) · 1.61 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
#include "main.h"
/**
* _printchar - prints character
*
* Return: Nothing
*
*/
void _printchar(void)
{
_putchar(va_arg(id.args, int));
id.count++;
}
/**
* _printstr - prints str
*
* Return: Nothing
*
*/
void _printstr(void)
{
id.str = va_arg(id.args, char *);
if (id.str == NULL)
id.str = "(null)";
else
{
id.j = 0;
while (id.str[id.j] != '\0')
{
_putchar(id.str[id.j]);
id.j++;
id.count++;
}
}
}
/**
* _printint - prints integer
*
* Return: void
*/
void _printint(void)
{
int num, div, len;
num = va_arg(id.args, int);
div = 1;
len = 0;
if (num < 0)
{
_putchar('-');
num = num * -1;
id.count++;
}
if (num == 0)
{
_putchar('0');
id.count++;
}
while (num / div > 0)
{
div = div * 10;
len++;
}
div = div / 10;
while (len > 0)
{
_putchar(((num / div) % 10) + '0');
div = div / 10;
len--;
id.count++;
}
}
/**
* _printf - produces output according to a format
* @format: character string
* Return: number of characters printed
*/
int _printf(const char *format, ...)
{
id.i = 0, id.j = 0, id.count = 0;
if (format == NULL)
return (-1);
va_start(id.args, format);
while (format[id.i] != '\0')
{
if (format[id.i] == '%')
{
id.i++;
if (format[id.i] == 'c')
_printchar();
else if (format[id.i] == 's')
_printstr();
else if (format[id.i] == 'd' || format[id.i] == 'i')
_printint();
else if (format[id.i] == '%')
_putchar('%');
else
{
_putchar('%');
_putchar(format[id.i]);
id.count += 2;
}
}
else
{
_putchar(format[id.i]);
id.count++;
}
id.i++;
}
va_end(id.args);
return (id.count);
}