1
0

fix strjoin & substr & make strtrim

This commit is contained in:
Adam Joly
2023-11-11 01:28:12 +01:00
parent 63184fceea
commit 960f8d50e5
7 changed files with 88 additions and 28 deletions

View File

@ -6,7 +6,7 @@
# By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ # # By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/11/01 11:03:22 by adjoly #+# #+# # # Created: 2023/11/01 11:03:22 by adjoly #+# #+# #
# Updated: 2023/11/09 19:22:28 by adjoly ### ########.fr # # Updated: 2023/11/11 01:04:23 by adjoly ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -44,6 +44,7 @@ SRCS = ft_atoi.c \
ft_strrchr.c \ ft_strrchr.c \
ft_strnstr.c \ ft_strnstr.c \
ft_putendl_fd.c \ ft_putendl_fd.c \
ft_strtrim.c \
OBJS = $(SRCS:.c=.o) OBJS = $(SRCS:.c=.o)

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 16:02:26 by adjoly #+# #+# */ /* Created: 2023/11/01 16:02:26 by adjoly #+# #+# */
/* Updated: 2023/11/05 22:23:07 by adjoly ### ########.fr */ /* Updated: 2023/11/11 00:55:15 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,7 +18,7 @@ void *ft_calloc(size_t nmemb, size_t size)
void *result; void *result;
if (size != 0 && nmemb != 0 && (nmemb * size) / nmemb != size) if (size != 0 && nmemb != 0 && (nmemb * size) / nmemb != size)
return (NULL); return (malloc(0));
result = malloc(size * nmemb); result = malloc(size * nmemb);
if (result == NULL) if (result == NULL)
return (NULL); return (NULL);

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 19:04:42 by adjoly #+# #+# */ /* Created: 2023/11/09 19:04:42 by adjoly #+# #+# */
/* Updated: 2023/11/10 17:05:46 by adjoly ### ########.fr */ /* Updated: 2023/11/11 00:36:54 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,23 +18,41 @@ static int ft_countword(char const *s, char sep)
int w_count; int w_count;
i = 0; i = 0;
if (s[i] != sep) w_count = 0;
w_count++;
while (s[i]) while (s[i])
{ {
if (s[i] == sep && s[i - 1] != sep) if (s[i] != sep && (i == 0 || s[i - 1] == sep))
w_count++; w_count++;
i++; i++;
} }
return (w_count); return (w_count);
} }
const int ft_countletter(char const *s, char sep)
{
int i;
i = 0;
while (s[i] != sep || s[i] != '\0')
i++;
return (i);
}
char **ft_split(char const *s, char c) char **ft_split(char const *s, char c)
{ {
int w_count; int w_count;
char **result; char **result;
w_count = ft_countword(s, c); w_count = ft_countword(s, c);
result = malloc(w_count); result = malloc((w_count + 1) * sizeof(char *));
if (result == NULL)
return (NULL);
while (s[i])
{
while ()
{
}
}
return (result); return (result);
} }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/04 13:44:09 by adjoly #+# #+# */ /* Created: 2023/11/04 13:44:09 by adjoly #+# #+# */
/* Updated: 2023/11/09 19:19:28 by adjoly ### ########.fr */ /* Updated: 2023/11/11 01:26:47 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,18 +14,28 @@
char *ft_strjoin(char const *s1, char const *s2) char *ft_strjoin(char const *s1, char const *s2)
{ {
// int i;
char *result; char *result;
size_t len_s1; size_t i;
size_t len_s2; size_t j;
// i = 0; i = 0;
len_s1 = ft_strlen(s1); j = 0;
len_s2 = ft_strlen(s2); if (s1 == NULL && s2 == NULL)
result = malloc(len_s1 + len_s2 + 1); return (NULL);
result = malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (result == NULL) if (result == NULL)
return (NULL); return (NULL);
ft_memcpy(result, s1, len_s1); while (s1[i])
ft_memcpy(result + len_s1, s2, len_s2); {
result[i] = s1[i];
i++;
}
while (s2[j])
{
result[i] = s2[j];
i++;
j++;
}
result[i] = '\0';
return (result); return (result);
} }

31
ft_strtrim.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/11 00:37:37 by adjoly #+# #+# */
/* Updated: 2023/11/11 01:09:36 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s1, char const *set)
{
int i;
int j;
char *result;
i = 0;
if (s1 == NULL || set == NULL)
return (NULL);
j = ft_strlen(s1);
while (ft_strchr(set, s1[i]) != 0)
i++;
while (ft_strrchr(set, s1[j]) != 0)
j--;
result = ft_substr(s1, i, (j - i) + 1);
return (result);
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/02 17:59:58 by adjoly #+# #+# */ /* Created: 2023/11/02 17:59:58 by adjoly #+# #+# */
/* Updated: 2023/11/10 17:13:29 by adjoly ### ########.fr */ /* Updated: 2023/11/11 01:03:32 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,16 +19,15 @@ char *ft_substr(char const *s, unsigned int start, size_t len)
i = 0; i = 0;
if (s == NULL) if (s == NULL)
return (NULL); return (0);
if (start > ft_strlen(s)) if (len >= ft_strlen(s))
{ len = ft_strlen(s) - start;
result = calloc(1, 1); if (len == 0 || start >= ft_strlen(s))
return (result); return (calloc(1, 1));
} result = malloc((len + 1) * sizeof(char));
result = malloc(((ft_strlen(s) - len) + 1) * sizeof(char));
if (result == NULL) if (result == NULL)
return (NULL); return (NULL);
while (i < len - 1 && s[start + i]) while (i < len && s[start + i])
{ {
result[i] = s[start + i]; result[i] = s[start + i];
i++; i++;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 10:06:03 by adjoly #+# #+# */ /* Created: 2023/11/01 10:06:03 by adjoly #+# #+# */
/* Updated: 2023/11/09 19:22:26 by adjoly ### ########.fr */ /* Updated: 2023/11/11 01:05:15 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -49,5 +49,6 @@ int ft_memcmp(const void *s1, const void *s2, size_t n);
char *ft_strrchr(const char *s, int c); char *ft_strrchr(const char *s, int c);
char *ft_strnstr(const char *big, const char *little, size_t len); char *ft_strnstr(const char *big, const char *little, size_t len);
void ft_putendl_fd(char *s, int fd); void ft_putendl_fd(char *s, int fd);
char *ft_strtrim(char const *s1, char const *set);
#endif #endif