libft
Loading...
Searching...
No Matches
ft_strtrim.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* ft_strtrim.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tspoof <tspoof@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2022/11/05 14:05:22 by tspoof #+# #+# */
9/* Updated: 2022/11/09 18:58:39 by tspoof ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "libft.h"
14
15char *ft_strtrim(char const *s1, char const *set)
16{
17 char *ptr;
18 size_t start_idx;
19 size_t end_idx;
20
21 if (!s1 || !set)
22 return (NULL);
23 start_idx = 0;
24 end_idx = ft_strlen(s1);
25 while (ft_strchr(set, s1[start_idx]) && start_idx < end_idx)
26 start_idx++;
27 while (ft_strchr(set, s1[end_idx]) && start_idx < end_idx)
28 end_idx--;
29 ptr = ft_substr(s1 + start_idx, 0, end_idx - start_idx + 1);
30 if (!ptr)
31 return (NULL);
32 return (ptr);
33}
char * ft_strtrim(char const *s1, char const *set)
Trim start and end of a string.
Definition ft_strtrim.c:15
char * ft_substr(char const *s, unsigned int start, size_t len)
Gets a piece of string out of a string.
Definition ft_substr.c:22
char * ft_strchr(const char *s, int c)
Search a character.
Definition ft_strchr.c:15
size_t ft_strlen(const char *s)
Length of the string.
Definition ft_strlen.c:15