-
Notifications
You must be signed in to change notification settings - Fork 1
/
_printf.c
129 lines (123 loc) · 1.93 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
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
124
125
126
127
128
129
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include "main.h"
/**
* print_string - something usefull
* @s: char
* @z: int
* Return: letter count
*/
int print_string(unsigned int z, char *s)
{
int i = 0;
if (!s)
{
_putchar('(');
_putchar('n');
_putchar('u');
_putchar('l');
_putchar('l');
_putchar(')');
z = z + 6;
}
else
{
while (*(s + i) != '\0')
{
_putchar(*(s + i));
i++;
z++;
}
}
return (z);
}
/**
* print_char - something usefull
* @c: char
* @z: int
* Return: letter count
*/
int print_char(unsigned int z, char c)
{
_putchar(c);
z++;
return (z);
}
/**
* print_decimal - This is decimal
* @value: First operand
* Return: 0 Successful
*/
int print_decimal(int value)
{
int i, j;
char buffer[1000000];
int printed = 0;
unsigned int n;
if (value < 0)
{
_putchar('-');
printed++;
n = -value;
}
else
{
n = value;
}
i = 0;
do {
buffer[i++] = '0' + (n % 10);
n /= 10;
printed++;
} while (n > 0);
for (j = i - 1; j >= 0; j--)
{
_putchar(buffer[j]);
}
return (printed);
}
/**
* _printf - print string
* @format: format
* Return: letter count
*/
int _printf(const char *format, ...)
{
va_list ptr;
unsigned int j = 0, r = 0;
if (!format)
{
exit(98);
}
if (*format == '%' && *(format + 1) == 0)
exit(98);
va_start(ptr, format);
while (*(format + j))
{
if (*(format + j) == '%' && *(format + j + 1) == '%')
{
_putchar('%'), r++, j += 2;
}
else if (*(format + j) == '%' && *(format + j + 1) == 'c')
{
r = print_char(r, (char)va_arg(ptr, int)), j += 2;
}
else if (*(format + j) == '%' && *(format + j + 1) == 's')
{
r = print_string(r, va_arg(ptr, char *)), j += 2;
}
else if (*(format + j) == '%' && (*(format + j + 1) == 'd' ||
*(format + j + 1) == 'i'))
{
r += print_decimal(va_arg(ptr, int)), j += 2;
}
else
{
r++, _putchar(*(format + j)), j++;
}
}
return (r);
}