-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
75 lines (69 loc) · 2.02 KB
/
ft_printf.c
File metadata and controls
75 lines (69 loc) · 2.02 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: algungor <algungor@student.42istanbul.com.t+#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/15 02:15:24 by algungor #+# #+# */
/* Updated: 2026/03/14 02:16:38 by algungor ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_find(char c, va_list *ac)
{
if (c == '%')
return (ft_putchar('%'));
if (c == 'c')
return (ft_putchar(va_arg(*ac, int)));
if (c == 's')
return (ft_putstr(va_arg(*ac, char *)));
if (c == 'd' || c == 'i')
return (ft_putnbr(va_arg(*ac, int)));
if (c == 'i')
return (ft_putnbr(va_arg(*ac, int)));
if (c == 'x' || c == 'X')
return (ft_x_control(va_arg(*ac, unsigned int), c));
if (c == 'p')
return (ft_p_control(va_arg(*ac, void *), c));
if (c == 'u')
return (ft_unnbr(va_arg(*ac, unsigned int)));
return (-1);
}
static int ft_percent(const char *format, int *i, va_list *ac)
{
int check;
(*i)++;
if (!format)
return (0);
check = ft_find(format[*i], ac);
(*i)++;
return (check);
}
int ft_printf(const char *format, ...)
{
int i;
int check;
int count;
va_list ac;
i = 0;
count = 0;
va_start(ac, format);
if (format[i] == '\0')
return (-1);
while (format[i])
{
check = 0;
if (format[i] != '%')
count += ft_putchar(format[i++]);
if (format[i] == '%')
{
check = ft_percent(format, &i, &ac);
if (check == -1)
return (-1);
}
count += check;
}
va_end(ac);
return (count);
}