-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask0.c
More file actions
55 lines (52 loc) · 1.45 KB
/
task0.c
File metadata and controls
55 lines (52 loc) · 1.45 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
#include "main.h"
/**
* _printf - Custom printf function with support for %c and %s specifiers
* @format: A format string with optional specifiers
* Return: The number of characters printed (excluding null byte)
*/
int _printf(const char *format, ...)
{
int input = 0; /* Count of characters printed */
va_list arguments; /* List of variable arguments */
char c; /* Character specifier */
int l; /* Length of string specifier */
char *str; /* String specifier */
if (!format)
return (-1); /* Return -1 for NULL format string */
va_start(arguments, format); /* Initialize variable arguments */
while (*format)
{
if (*format != '%')
{
write(1, format, 1); /* Write non-specifier characters */
input++;
} else
{
format++;
switch (*format)
{
case '\0':
break; /* Ignore trailing % */
case '%':
write(1, format, 1); /* Write a literal % */
input++;
break;
case 'c':
c = va_arg(arguments, int);
write(1, &c, 1); /* Write character specifier */
input++;
break;
case 's':
str = va_arg(arguments, char*);
l = strlen(str);
write(1, str, l); /* Write string specifier */
input += l;
break;
default:
break; /* Handle invalid format specifiers */
}
} format++;
}
va_end(arguments); /* Clean up variable arguments */
return (input); /* Return the total number of characters printed */
}