-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
64 lines (59 loc) · 1.58 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: galemair <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/03 16:19:57 by galemair #+# #+# */
/* Updated: 2018/04/04 14:03:01 by galemair ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t get_size(char const *s)
{
size_t len;
size_t i;
size_t j;
i = 0;
len = 0;
while (s[i] == ' ' || s[i] == '\n' || s[i] == '\t')
i++;
while (s[i])
{
j = 0;
while (s[i + j] == ' ' || s[i + j] == '\n'
|| s[i + j] == '\t' || s[i + j] == '\0')
{
if (s[i + j] == '\0')
return (len);
j++;
}
len++;
i++;
}
return (len);
}
char *ft_strtrim(char const *s)
{
char *str;
size_t size;
size_t i;
size_t j;
i = 0;
j = 0;
if (!s)
return (NULL);
size = get_size(s);
if ((str = ft_strnew(size)) == NULL)
return (NULL);
while (s[i] == ' ' || s[i] == '\n' || s[i] == '\t')
i++;
while (j < size)
{
str[j] = s[i];
j++;
i++;
}
return (str);
}