end of mandatory functions

This commit is contained in:
David Gailleton
2025-11-08 17:57:58 +01:00
parent 25a804847b
commit b603db3539
13 changed files with 486 additions and 2 deletions

View File

@@ -32,7 +32,18 @@ SRC = ft_isalpha.c \
ft_strnstr.c \
ft_atoi.c \
ft_calloc.c \
ft_strdup.c
ft_strdup.c \
ft_substr.c \
ft_strjoin.c \
ft_strtrim.c \
ft_split.c \
ft_itoa.c \
ft_strmapi.c \
ft_striteri.c \
ft_putchar_fd.c \
ft_putstr_fd.c \
ft_putendl_fd.c \
ft_putnbr_fd.c
OBJ_DIR = objs

75
ft_itoa.c Normal file
View File

@@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/07 13:06:35 by dgaillet #+# #+# */
/* Updated: 2025/11/07 13:52:20 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t malloc_size(int n)
{
size_t size;
size = 0;
if (n <= 0)
size++;
while (n)
{
n /= 10;
size++;
}
return (size);
}
void insert_char(char *str, unsigned int nbr, int index)
{
if (!index)
return ;
str[index] = (nbr % 10) + '0';
insert_char(str, nbr / 10, index - 1);
}
char *ft_itoa(int n)
{
unsigned int nbr;
char *str;
nbr = n;
if (n < 0)
nbr = n * -1;
str = malloc(sizeof(char) * (malloc_size(n) + 1));
if (!str)
return (NULL);
if (nbr == 0)
str[0] = '0';
else
{
if (n < 0)
{
str[0] = '-';
}
insert_char(str, nbr, malloc_size(n) - 1);
}
str[malloc_size(n)] = '\0';
return (str);
}
#include "stdio.h"
int main(int argc, char **argv)
{
char *str;
if (argc > 1)
{
str = ft_itoa(atoi(argv[1]));
printf("%s\n", str);
free(str);
}
}

18
ft_putchar_fd.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/08 17:41:14 by dgaillet #+# #+# */
/* Updated: 2025/11/08 17:42:19 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}

20
ft_putendl_fd.c Normal file
View File

@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/08 17:46:08 by dgaillet #+# #+# */
/* Updated: 2025/11/08 17:47:46 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <unistd.h>
void ft_putendl_fd(char *s, int fd)
{
ft_putstr_fd(s, fd);
write(fd, "\n", 1);
}

35
ft_putnbr_fd.c Normal file
View File

@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/08 17:50:35 by dgaillet #+# #+# */
/* Updated: 2025/11/08 17:57:02 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void write_nbr(unsigned int n, int fd)
{
if (n)
{
write_nbr(n / 10, fd);
ft_putchar_fd(n % 10 + '0', fd);
}
}
void ft_putnbr_fd(int n, int fd)
{
if (!n)
ft_putchar_fd(n % 10 + '0', fd);
else if (n < 0)
{
ft_putchar_fd('-', fd);
write_nbr(n * -1, fd);
}
else
write_nbr(n, fd);
}

22
ft_putstr_fd.c Normal file
View File

@@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/08 17:43:42 by dgaillet #+# #+# */
/* Updated: 2025/11/08 17:44:38 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putstr_fd(char *s, int fd)
{
while (*s)
{
ft_putchar_fd(*s, fd);
s++;
}
}

84
ft_split.c Normal file
View File

@@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/06 17:02:58 by dgaillet #+# #+# */
/* Updated: 2025/11/07 13:05:00 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t strs_size(const char *s, char c)
{
int i;
int j;
i = 0;
j = 0;
while (s[j])
{
if ((s[j] != c && j == 0) || (s[j] != c && s[j - 1] == c))
i++;
j++;
}
return (i);
}
int next_match(char *str, char c)
{
int i;
i = 0;
while (str[i] != c)
i++;
return (i);
}
char **ft_split(char const *s, char c)
{
char **strs;
int i;
int j;
strs = malloc(sizeof(char *) * (strs_size(s, c) + 1));
if (!strs)
return (free(strs), NULL);
i = 0;
j = 0;
while (s[j])
{
if ((s[j] != c && j == 0) || (s[j] != c && s[j - 1] == c))
{
if (s[j] == c)
j++;
strs[i] = ft_substr(&s[j], 0, next_match((char *) &s[j], c));
i++;
}
j++;
}
strs[i] = NULL;
return (strs);
}
/*
#include <stdio.h>
int main(void)
{
char **strs;
int i;
strs = ft_split("awdawdawd", ' ');
i = 0;
while (strs[i])
{
printf("%s\n", strs[i]);
free(strs[i]);
i++;
}
free (strs);
}
*/

23
ft_striteri.c Normal file
View File

@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/08 17:36:38 by dgaillet #+# #+# */
/* Updated: 2025/11/08 17:40:42 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
void ft_striteri(char *s, void (*f)(unsigned int, char*))
{
unsigned int i;
i = 0;
while (s[i])
{
f(i, &s[i]);
i++;
}
}

38
ft_strjoin.c Normal file
View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/06 13:52:21 by dgaillet #+# #+# */
/* Updated: 2025/11/06 13:56:29 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
char *str;
int i;
int j;
str = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
if (!str)
return (NULL);
i = 0;
j = 0;
while (s1[i])
{
str[i] = s1[i];
i++;
}
while (s2[j])
{
str[i + j] = s2[j];
j++;
}
str[i + j] = '\0';
return (str);
}

31
ft_strmapi.c Normal file
View File

@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/08 17:30:25 by dgaillet #+# #+# */
/* Updated: 2025/11/08 17:34:12 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
char *str;
unsigned int i;
i = 0;
str = malloc(sizeof(char) * (ft_strlen(s) + 1));
if (!str)
return (NULL);
while (s[i])
{
str[i] = f(i, s[i]);
i++;
}
str[i] = '\0';
return (str);
}

79
ft_strtrim.c Normal file
View File

@@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/06 16:31:38 by dgaillet #+# #+# */
/* Updated: 2025/11/06 17:02:34 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int test_charset(char c, char const *set)
{
if (!set[0])
return (0);
if (c == set[0])
return (1);
return (test_charset(c, set + 1));
}
int end_trim(char const *s1, char const *set)
{
int i;
i = 0;
if (!s1[0])
return (0);
while (s1[i])
i++;
i--;
while (test_charset(s1[i], set))
i--;
return (i);
}
int start_trim(char const *s1, char const *set)
{
int i;
i = 0;
while (test_charset(s1[i], set))
i++;
return (i);
}
char *ft_strtrim(char const *s1, char const *set)
{
int start;
int end;
int i;
char *str;
i = 0;
start = start_trim(s1, set);
end = end_trim(s1, set);
str = malloc(sizeof(char) * (end - start + 2));
if (!str)
return (NULL);
while (start + i <= end)
{
str[i] = s1[i + start];
i++;
}
str[i] = '\0';
return (str);
}
/*
int main(void)
{
char *test;
test = ft_strtrim("lorem \n ipsum \t dolor \n sit \t amet", " ");
printf("%s\n", test);
free(test);
}
*/

37
ft_substr.c Normal file
View File

@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/06 13:20:55 by dgaillet #+# #+# */
/* Updated: 2025/11/06 13:58:18 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
char *sub_str;
size_t s_len;
s_len = ft_strlen(s);
if (start > s_len)
len = 0;
else if (s_len < (start + len))
len = s_len - start;
sub_str = malloc(sizeof(char) * (len + 1));
if (!sub_str)
return (NULL);
i = 0;
while (i < len)
{
sub_str[i] = s[start + i];
i++;
}
sub_str[i] = '\0';
return (sub_str);
}

13
libft.h
View File

@@ -6,7 +6,7 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 09:49:43 by dgaillet #+# #+# */
/* Updated: 2025/11/05 21:10:02 by dgaillet ### ########lyon.fr */
/* Updated: 2025/11/08 17:56:20 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
@@ -38,5 +38,16 @@ char *ft_strnstr(const char *big, const char *little, size_t len);
int ft_atoi(const char *nptr);
void *ft_calloc(size_t nmemb, size_t size);
char *ft_strdup(const char *s);
char *ft_substr(char const *s, unsigned int start, size_t len);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_strtrim(char const *s1, char const *set);
char **ft_split(char const *s, char c);
char *ft_itoa(int n);
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
void ft_striteri(char *s, void (*f)(unsigned int, char*));
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *s, int fd);
void ft_putendl_fd(char *s, int fd);
void ft_putnbr_fd(int n, int fd);
#endif