sprintf
This commit is contained in:
64
sprintf/ft_sprintf.c
Normal file
64
sprintf/ft_sprintf.c
Normal file
@@ -0,0 +1,64 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_sprintf.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/24 13:46:45 by lderidde #+# #+# */
|
||||
/* Updated: 2025/01/24 13:46:45 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_sprintf.h"
|
||||
|
||||
static int is_spec(char c)
|
||||
{
|
||||
if (c == 'c' || c == 's' || c == 'd')
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static char *add_arg(char c, char *str, va_list args)
|
||||
{
|
||||
if (c == 'c')
|
||||
return (ft_strfjoinc(str, va_arg(args, int)));
|
||||
else if (c == 's')
|
||||
return (ft_strfjoin(str, va_arg(args, char *)));
|
||||
else if (c == 'd')
|
||||
return (ft_strfjoind(str, ft_itoa(va_arg(args, int))));
|
||||
}
|
||||
|
||||
static char *build_str(const char *str, va_list args)
|
||||
{
|
||||
char *out;
|
||||
int i;
|
||||
|
||||
out = NULL;
|
||||
i = 0;
|
||||
while (str[i])
|
||||
{
|
||||
if (str[i] == '%' && str[i + 1])
|
||||
{
|
||||
if (is_spec(str[++i]))
|
||||
out = add_arg(str[i], out, args);
|
||||
else
|
||||
out = ft_strfjoinc(out, '%');
|
||||
}
|
||||
else
|
||||
out = ft_strfjoinc(out, str[i]);
|
||||
i++;
|
||||
}
|
||||
return (out);
|
||||
}
|
||||
|
||||
char *ft_sprintf(const char *str, ...)
|
||||
{
|
||||
va_list args;
|
||||
char *out;
|
||||
|
||||
va_start(args, str);
|
||||
out = build_str(str, args);
|
||||
va_end(args);
|
||||
return (out);
|
||||
}
|
||||
25
sprintf/ft_sprintf.h
Normal file
25
sprintf/ft_sprintf.h
Normal file
@@ -0,0 +1,25 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_sprintf.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/24 13:44:08 by lderidde #+# #+# */
|
||||
/* Updated: 2025/01/24 13:47:01 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FT_SPRINTF_H
|
||||
# define FT_SPRINTF_H
|
||||
|
||||
# include <stdlib.h>
|
||||
# include <stdarg.h>
|
||||
# include <stdio.h>
|
||||
# include "../lib/libft/libft.h"
|
||||
|
||||
char *ft_strfjoin(char *s1, char *s2);
|
||||
char *ft_strfjoinc(char *s1, char c);
|
||||
char *ft_strfjoind(char *s1, char *s2);
|
||||
|
||||
#endif
|
||||
92
sprintf/sprintf_utils.c
Normal file
92
sprintf/sprintf_utils.c
Normal file
@@ -0,0 +1,92 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sprintf_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/24 13:44:22 by lderidde #+# #+# */
|
||||
/* Updated: 2025/01/24 13:47:10 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_sprintf.h"
|
||||
|
||||
char *ft_strfjoin(char *s1, char *s2)
|
||||
{
|
||||
char *out;
|
||||
unsigned int i;
|
||||
unsigned int j;
|
||||
|
||||
if (!s1 && !s2)
|
||||
return (NULL);
|
||||
if (!s1)
|
||||
return (ft_strdup(s2));
|
||||
if (!s2)
|
||||
return (ft_strdup(s1));
|
||||
out = ft_calloc(ft_strlen(s1) + ft_strlen(s2) + 1, sizeof(char));
|
||||
if (!out)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (s1[j])
|
||||
out[i++] = s1[j++];
|
||||
j = 0;
|
||||
while (s2[j])
|
||||
out[i++] = s2[j++];
|
||||
ft_free(&s1);
|
||||
return (out);
|
||||
}
|
||||
|
||||
char *ft_strfjoinc(char *s1, char c)
|
||||
{
|
||||
char *out;
|
||||
unsigned int i;
|
||||
unsigned int j;
|
||||
|
||||
if (!s1)
|
||||
{
|
||||
out = ft_calloc(2, sizeof(char));
|
||||
if (!out)
|
||||
return (NULL);
|
||||
out[0] = c;
|
||||
return (out);
|
||||
}
|
||||
out = ft_calloc(ft_strlen(s1) + 2, sizeof(char));
|
||||
if (!out)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (s1[j])
|
||||
out[i++] = s1[j++];
|
||||
out[i] = c;
|
||||
ft_free(&s1);
|
||||
return (out);
|
||||
}
|
||||
|
||||
char *ft_strfjoind(char *s1, char *s2)
|
||||
{
|
||||
char *out;
|
||||
unsigned int i;
|
||||
unsigned int j;
|
||||
|
||||
if (!s1 && !s2)
|
||||
return (NULL);
|
||||
if (!s1)
|
||||
return (ft_strdup(s2));
|
||||
if (!s2)
|
||||
return (ft_strdup(s1));
|
||||
out = ft_calloc(ft_strlen(s1) + ft_strlen(s2) + 1, sizeof(char));
|
||||
if (!out)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (s1[j])
|
||||
out[i++] = s1[j++];
|
||||
j = 0;
|
||||
while (s2[j])
|
||||
out[i++] = s2[j++];
|
||||
ft_free(&s1);
|
||||
ft_free(&s2);
|
||||
return (out);
|
||||
}
|
||||
Reference in New Issue
Block a user