Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 8 additions & 36 deletions _printf.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
#include "main.h"
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>

/**
* _printf - a function that produces output according to a format.
*
* @format: format specifier
*
* Return: Always 0 (Success)
*/
int putch(char c);
int putss(char *s);

int _printf(const char *format, ...)
{
unsigned int i, counter =0, s_counter = 0;
unsigned int i, counter, s_counter = 0;

va_list words;

Expand All @@ -27,50 +23,26 @@ int _printf(const char *format, ...)
{
if (format[i] != '%')
{
putchar(format[i]);
counter++;
putch(format[i]);
s_counter++;
}
else if (format[i] == '%' && format[i + 1] == 'c')
{
char c = va_arg(words,int);
putch(c);
putch(va_arg(words, int));
i++;
counter++;
}
else if (format[i] == '%' && format[i + 1] == 's')
{
char *s = va_arg(words,char *);
counter += putss(s);
s_counter = putss(va_arg(words, char *));
i++;

counter += (s_counter - 1);
}
else if (format[i] == '%' && format[i + 1] == '%')
{
putch('%');
i++;
counter++;
}
counter++;
}

va_end(words);
return (counter);
}

int putch(char c)
{
return (write(1, &c, 1));
}

int putss(char *s)
{
int counter = 0;

if (s)
{
for (counter = 0; s[counter] != '\0'; counter++)
{
putch(s[counter]);
}
}
return (counter);
}