diff --git a/main.h b/main.h index feaa492..bcf973d 100644 --- a/main.h +++ b/main.h @@ -3,6 +3,7 @@ int _printf(const char *format, ...); int _contains(const char *str, char c); int _strlen(const char *str); +char *_strcpy(char *dest, char *src); #endif diff --git a/printf.c b/printf.c index 8d74049..13f533c 100644 --- a/printf.c +++ b/printf.c @@ -11,7 +11,7 @@ **/ int _printf(const char *format, ...) { - int i, j; + int buff_idx; unsigned int identifiers, BUFF_SIZE; char *buffer, *next; va_list args; @@ -20,22 +20,21 @@ int _printf(const char *format, ...) va_start(args, format); identifiers = _contains(format, '%'); BUFF_SIZE = _strlen(format) - (identifiers * 2); + buffer = malloc(BUFF_SIZE); if (!format) /* No string. No laundry */ return (0); - i = 0; /*TODO:gotta rename those*/ - j = 0; + buff_idx = 0; while (format) { - if ((*format == '%') && (*(format + 1))) + if ((*format == '%') && (*(format + 1))) /*hello %s*/ { switch (*(format + 1)) /*this needs to shrink*/ { case 's': next = va_arg(args, char*); /*Store string temporarily*/ - _strcpy(&buffer[j], next); - j += _strlen(next); - BUFF_SIZE += _strlen(next); + buffer = _strcpy(buffer, next); + BUFF_SIZE += _strlen(buffer); break; case 'c': /* add 1 byte and i++ */ @@ -46,11 +45,11 @@ int _printf(const char *format, ...) } else { - *(buffer + i) = *(format + i); - i++; + *(buffer + buff_idx) = *(format + buff_idx); + buff_idx++; } } - write(1, buffer, BUFF_SIZE); + write(1, &buffer, BUFF_SIZE); return (_strlen(buffer)); } diff --git a/strings.c b/strings.c index 0f1422e..da4b1c7 100644 --- a/strings.c +++ b/strings.c @@ -5,14 +5,14 @@ * * @str: Address to the head of the string (Array of Characters) (Lost btw) * - * Returns: Length of String + * Return: Length of String */ int _strlen(const char *str) { - if (*str) - return (1 + _strlen(++str)); - else - return (0); + if (*str) + return (1 + _strlen(++str)); + else + return (0); } /** @@ -25,13 +25,13 @@ int _strlen(const char *str) */ int _contains(const char *str, char c) { - if (*str) - { - if (*str == c && *(str + 1) != c && *(str - 1) != c) - return (1 + _contains(str + 1, c)); - else - return (_contains(str + 1, c)); - } - else - return (0); + if (*str) + { + if (*str == c && *(str + 1) != c && *(str - 1) != c) + return (1 + _contains(str + 1, c)); + else + return (_contains(str + 1, c)); + } + else + return (0); } diff --git a/tests/str_print.c b/tests/str_print.c index 951dd74..0585c23 100644 --- a/tests/str_print.c +++ b/tests/str_print.c @@ -1,5 +1,5 @@ -#include<../main.h> -#include +#include "../main.h" +#include /** * main - Tests if '%s' works within our printf. @@ -10,10 +10,9 @@ int main(void) { char test_0[] = "Hej"; - char test_1[] = "H,e,j"; /* * Brain ded to think bout test cond */ + return (_printf("%s", test_0)); - return (0); }