maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
ft_strtrim.c
Go to the documentation of this file.
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* ft_strtrim.c :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: rmikhayl <rmikhayl@student.42london.c +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2023/12/17 17:25:26 by rmikhayl #+# #+# */
9
/* Updated: 2023/12/17 17:25:26 by rmikhayl ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
13
#include "
libft.h
"
14
15
/* *************************** ft_strtrim *********************************** */
16
/* Trims leading and trailing characters from 's1' that match any character */
17
/* in 'set.' Returns a new string with trimmed content or NULL if memory */
18
/* allocation fails. */
19
/* */
20
/* In layman's terms: It's like removing certain characters from the */
21
/* beginning and end of a sentence, creating a new sentence without them. */
22
/* If something goes wrong during this process, you get nothing. */
23
/* ************************************************************************** */
24
25
static
int
ft_char_in_set
(
char
c,
char
const
*set)
26
{
27
size_t
i;
28
29
i = 0;
30
while
(set[i])
31
{
32
if
(set[i] == c)
33
return
(1);
34
i++;
35
}
36
return
(0);
37
}
38
39
char
*
ft_strtrim
(
char
const
*s1,
char
const
*set)
40
{
41
char
*str;
42
size_t
i;
43
size_t
start;
44
size_t
end;
45
46
if
(!s1 || !set)
47
return
(NULL);
48
start = 0;
49
while
(s1[start] &&
ft_char_in_set
(s1[start], set))
50
start++;
51
end =
ft_strlen
(s1);
52
while
(end > start &&
ft_char_in_set
(s1[end - 1], set))
53
end--;
54
str = (
char
*)malloc(
sizeof
(*s1) * (end - start + 1));
55
if
(!str)
56
return
(NULL);
57
i = 0;
58
while
(start < end)
59
str[i++] = s1[start++];
60
str[i] = 0;
61
return
(str);
62
}
ft_strtrim
char * ft_strtrim(char const *s1, char const *set)
Definition
ft_strtrim.c:39
ft_char_in_set
static int ft_char_in_set(char c, char const *set)
Definition
ft_strtrim.c:25
libft.h
ft_strlen
size_t ft_strlen(const char *s)
Definition
ft_strlen.c:15
lib
libft
src
ft_strtrim.c
Generated by
1.9.8