ft_strtrim: initial implementation
This commit is contained in:
parent
c795a9bab1
commit
5ec841de7f
3 changed files with 50 additions and 2 deletions
5
Makefile
5
Makefile
|
|
@ -6,7 +6,7 @@
|
|||
# By: kcolin <marvin@42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/10/14 13:43:59 by kcolin #+# #+# #
|
||||
# Updated: 2024/10/17 12:03:18 by kcolin ### ########.fr #
|
||||
# Updated: 2024/10/17 12:36:39 by kcolin ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
|
|
@ -36,7 +36,8 @@ SOURCES = ft_isalpha.c \
|
|||
ft_calloc.c \
|
||||
ft_strdup.c \
|
||||
ft_substr.c \
|
||||
ft_strjoin.c
|
||||
ft_strjoin.c \
|
||||
ft_strtrim.c
|
||||
OBJECTS = $(SOURCES:.c=.o)
|
||||
CC = gcc
|
||||
|
||||
|
|
|
|||
46
ft_strtrim.c
Normal file
46
ft_strtrim.c
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strtrim.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/17 12:36:52 by kcolin #+# #+# */
|
||||
/* Updated: 2024/10/17 14:06:29 by kcolin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
char *ft_strtrim(char const *s1, char const *set)
|
||||
{
|
||||
size_t i;
|
||||
size_t start;
|
||||
char *out;
|
||||
|
||||
i = 0;
|
||||
while (s1[i] != '\0' && ft_strchr(set, s1[i]) != NULL)
|
||||
i++;
|
||||
start = i;
|
||||
i = ft_strlen(s1);
|
||||
while (i > 0 && ft_strchr(set, s1[i - 1]) != NULL)
|
||||
i--;
|
||||
out = ft_substr(s1, start, i - start);
|
||||
return (out);
|
||||
}
|
||||
|
||||
/*
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc > 2)
|
||||
{
|
||||
char *trimmed = ft_strtrim(argv[1], argv[2]);
|
||||
printf("'%s'\n", trimmed);
|
||||
free(trimmed);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
*/
|
||||
1
libft.h
1
libft.h
|
|
@ -49,5 +49,6 @@ 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);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue