diff --git a/main.h b/main.h index bcf973d..ce36133 100644 --- a/main.h +++ b/main.h @@ -4,6 +4,9 @@ int _printf(const char *format, ...); int _contains(const char *str, char c); int _strlen(const char *str); char *_strcpy(char *dest, char *src); - +char *append(char *str, char c); +int _puts(char *str); +int _putchar(char c); +char* _memset(char *str, int bval); #endif diff --git a/memset.c b/memset.c new file mode 100644 index 0000000..4758670 --- /dev/null +++ b/memset.c @@ -0,0 +1,22 @@ +#include "main.h" + +/** + * _memset - sets values of bytes to specific value + * + * @adr: head address + * @n: number of bytes + * + * Return: pointer to place + */ + +char* _memset(char *str, int bval) +{ + int i; + + for (i = 0; i < _strlen(str); i++) + { + *(str + i) = bval; + } + + return (str); +} diff --git a/printf.c b/printf.c index c96a1e9..f9a1bea 100644 --- a/printf.c +++ b/printf.c @@ -10,44 +10,61 @@ **/ int _printf(const char *format, ...) { - int buff_idx = 0; - int fmt_idx = 0; - unsigned int identifiers = _contains(format, '%'); - unsigned int BUFF_SIZE = _strlen(format) - (identifiers * 2); - char *buffer, *next; + int buff_idx, fmt_idx; /* Indexes */ + unsigned int identifiers, BUFF_SIZE, printed; + char *buffer; /*where non formated things are stored*/ va_list args; va_start(args, format); - buffer = malloc(BUFF_SIZE); + + identifiers = _contains(format, '%'); /* instances of %s, %c etc */ + BUFF_SIZE = _strlen(format) - identifiers; + buffer = malloc(BUFF_SIZE); /* sized of the non % instances only*/ if (!format) /* No string. No laundry */ return (0); + + buff_idx = 0; /* was there a way to squish these together? */ + fmt_idx = 0; + printed = 0; while (*(format + fmt_idx)) { if ((*(format + fmt_idx) == '%') && (*(format + fmt_idx + 1))) { - switch (*(format + 1)) /*this needs to shrink*/ + if (*buffer) /* printing and clearing buffer on formatted things */ + { + _puts(buffer); + BUFF_SIZE -= _strlen(buffer); + printed += _strlen(buffer); + free(buffer); + buffer = malloc(BUFF_SIZE); + buff_idx = 0; + } + switch (*(format + fmt_idx + 1)) /*this needs to shrink*/ { case 's': - next = va_arg(args, char*); /*Store string temporarily*/ - buffer = _strcpy(buffer, next); - BUFF_SIZE = _strlen(buffer); - buff_idx += _strlen(next); - fmt_idx += 2; + printed += _puts(va_arg(args, char*)); break; - case 'c': /* add 1 byte and i++ */ + case 'c': + printed += _putchar(va_arg(args, int)); break; case '%': /*add 1 byte*/ + printed += _putchar('%'); break; } + fmt_idx += 2; } else { - *(buffer + buff_idx) = *(format + fmt_idx); + *(buffer + buff_idx) = *(format + fmt_idx); /* filling up buffer */ buff_idx++; fmt_idx++; } } - write(1, buffer, BUFF_SIZE + 1); - return (_strlen(buffer)); + if (*buffer) /*final buffer check*/ + { + printed += _puts(buffer); + free(buffer); + } + return (printed); } diff --git a/puts.c b/puts.c new file mode 100644 index 0000000..acb5018 --- /dev/null +++ b/puts.c @@ -0,0 +1,26 @@ +#include "main.h" +#include + +/** + * _puts - splurges whatever is in buffer + * + * @str: pointers to "buffer" or string head + * + * Return: written bytes + */ + +int _puts(char *str) +{ + return (write(1, str, _strlen(str))); +} +/** + * _putchar - writes the character c to stdout + * @c: The character to print + * + * Return: On success 1. + * On error, -1 is returned, and errno is set appropriately. + */ +int _putchar(char c) +{ + return (write(1, &c, 1)); +} diff --git a/strings.c b/strings.c index da4b1c7..ea662e1 100644 --- a/strings.c +++ b/strings.c @@ -1,4 +1,5 @@ #include "main.h" +#include /** * _strlen - Takes string and return its length @@ -35,3 +36,29 @@ int _contains(const char *str, char c) else return (0); } + +/** + * append - shoved character at the end + * + * @str: main string + * @c: character getting shoved + * + * Return: da string but appended + */ + +char *append(char *str, char c) +{ + int len, i; + char *new_me; + + len = _strlen(str); + + new_me = malloc(len + 1); + i = -1; + while (*(str + ++i)) + *(new_me + i) = *(str + i); + + *(new_me + i++) = c; + *(new_me + len + 1) = '\0'; + return (new_me); +} diff --git a/tests/append.c b/tests/append.c new file mode 100644 index 0000000..f62f16a --- /dev/null +++ b/tests/append.c @@ -0,0 +1,17 @@ +#include "../main.h" +#include + +/** + *main - ensure append work + */ +int main(void) +{ + char *str, c; + + str = "hello"; + c = 'c'; + str = append(str, c); + _printf("%s\n", str); + return (0); + +} diff --git a/tests/char_print.c b/tests/char_print.c new file mode 100644 index 0000000..d62a66e --- /dev/null +++ b/tests/char_print.c @@ -0,0 +1,15 @@ +#include "../main.h" + +/** + * main - testing printing of only char + * + * Return: deadth + */ + +int main(void) +{ + char c = 97; + + _printf("cat: %css\n", c); + return (0); +} diff --git a/tests/puts.c b/tests/puts.c new file mode 100644 index 0000000..fa57b26 --- /dev/null +++ b/tests/puts.c @@ -0,0 +1,12 @@ +#include "../main.h" +/** + * main - puts works? + */ +int main(void) +{ + char *str; + + str = "hello\n"; + _puts(str); + return (0); +} diff --git a/tests/strcpy.c b/tests/strcpy.c index d2edc4b..625d9d9 100644 --- a/tests/strcpy.c +++ b/tests/strcpy.c @@ -1,17 +1,21 @@ #include "../main.h" #include #include +#include /** * main - why does this not work! I know! * Return: 0...unless? */ int main(void) { - char *str, *str2; + char *str, *str2, c; str = "hello"; str2 = " world!\n"; str = _strcpy(str, str2); + c = 'c'; + printf("%s", str); + write(1, &c, 1); return (0); }