5
main.h
5
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
|
||||
|
||||
22
memset.c
Normal file
22
memset.c
Normal file
@@ -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);
|
||||
}
|
||||
49
printf.c
49
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);
|
||||
}
|
||||
|
||||
26
puts.c
Normal file
26
puts.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "main.h"
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* _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));
|
||||
}
|
||||
27
strings.c
27
strings.c
@@ -1,4 +1,5 @@
|
||||
#include "main.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* _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);
|
||||
}
|
||||
|
||||
17
tests/append.c
Normal file
17
tests/append.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "../main.h"
|
||||
#include <stdio.h>
|
||||
|
||||
/**
|
||||
*main - ensure append work
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
char *str, c;
|
||||
|
||||
str = "hello";
|
||||
c = 'c';
|
||||
str = append(str, c);
|
||||
_printf("%s\n", str);
|
||||
return (0);
|
||||
|
||||
}
|
||||
15
tests/char_print.c
Normal file
15
tests/char_print.c
Normal file
@@ -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);
|
||||
}
|
||||
12
tests/puts.c
Normal file
12
tests/puts.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "../main.h"
|
||||
/**
|
||||
* main - puts works?
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
char *str;
|
||||
|
||||
str = "hello\n";
|
||||
_puts(str);
|
||||
return (0);
|
||||
}
|
||||
@@ -1,17 +1,21 @@
|
||||
#include "../main.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user