-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
93 lines (81 loc) · 1.41 KB
/
_printf.c
File metadata and controls
93 lines (81 loc) · 1.41 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
#include "main.h"
#include <stdarg.h>
int _matchSpec(char c);
void slice(const char *str, char *result, int start, int end);
/**
* _printf - prints a formatted string to stdout
* @format: The format of the output
*
* Return: Number of characters printed
*/
int _printf(const char *format, ...)
{
int i = 0;
int n = 0;
int x = 0;
char c;
va_list args;
int sp = 0;
int si = 0;
char result[SIZE];
va_start(args, format);
for (; i < _strlen(format); i++)
{
c = format[i];
if (c == '%')
{
x = i;
for (x++; x < _strlen(format); x++)
{
if (format[x] == '-')
si = 2;
else if (format[x] == '+')
si = 1;
if (_matchSpec(format[x]))
{
slice(format, result, i + 1, x);
sp = _atoi(result);
n += _print_format(format[x], args, sp, si);
i = x;
break;
}
n++;
}
continue;
}
n++;
_putchar(c);
}
va_end(args);
return (n);
}
/**
* _matchSpec - match to specifiers
* @c: Character
* Return: 1 or 0 for if matched or not
*/
int _matchSpec(char c)
{
char arr[6] = "cs%dbi";
int i = 0;
for (; i < 6; i++)
{
if (c == arr[i])
{
return (1);
}
}
return (0);
}
/**
* slice - get part of a string
* @str: string to splice
* @result: spliced string
* @start: index to start from
* @end: final index
* Return: Nothing
*/
void slice(const char *str, char *result, int start, int end)
{
_strncpy(result, str + start, end - start);
}