This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
executable file
·50 lines (47 loc) · 1.64 KB
/
ft_strtrim.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_strtrim.c :+: :+: */
/* +:+ */
/* By: fbes <[email protected]> +#+ */
/* +#+ */
/* Created: 2020/10/27 15:10:24 by fbes #+# #+# */
/* Updated: 2022/02/08 19:48:05 by fbes ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
/**
* Trim a string on the left and the right, removing a set of characters,
* into a newly allocated string
* @param[in] *s1 The string to trim
* @param[in] *set A set of characters to remove from the string at the beginning
* and end
* @return A pointer to the trimmed string
*/
char *ft_strtrim(char const *s1, char const *set)
{
char *dest;
unsigned int start;
size_t dest_len;
size_t s1_len;
s1_len = ft_strlen(s1);
start = 0;
while (s1[start] != '\0')
{
if (ft_strchr(set, (int)s1[start]))
start++;
else
break ;
}
dest_len = s1_len - (size_t)start;
while (s1_len > 0)
{
if (ft_strchr(set, (int)s1[s1_len]))
dest_len--;
else
break ;
s1_len--;
}
dest = ft_substr(s1, start, dest_len + 1);
return (dest);
}