Libft
42 Libft library documentation
Loading...
Searching...
No Matches
ft_strtrim.c File Reference
+ Include dependency graph for ft_strtrim.c:

Go to the source code of this file.

Functions

char * ft_strtrim (char const *s1, char const *set)
 Trims leading and trailing characters from a set.
 

Function Documentation

◆ ft_strtrim()

char * ft_strtrim ( char const *  s1,
char const *  set 
)

Trims leading and trailing characters from a set.

Parameters
s1Source string.
setSet of characters to trim.
Returns
Newly allocated trimmed string, or NULL on failure.

Definition at line 22 of file ft_strtrim.c.

23{
24 size_t start;
25 size_t end;
26
27 if (!s1 || !set)
28 return (NULL);
29 start = 0;
30 while (s1[start] && ft_strchr(set, s1[start]))
31 start++;
32 end = ft_strlen(s1);
33 while (end > start && ft_strchr(set, s1[end - 1]))
34 end--;
35 return (ft_substr(s1, start, end - start));
36}
char * ft_substr(char const *s, unsigned int start, size_t len)
Extracts a substring from a string.
Definition ft_substr.c:23
size_t ft_strlen(const char *str)
Returns the length of a null-terminated string.
Definition ft_strlen.c:21
char * ft_strchr(const char *s, int c)
Finds the first occurrence of a character in a string.
Definition ft_strchr.c:24

References ft_strchr(), ft_strlen(), and ft_substr().

+ Here is the call graph for this function: