-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
72 lines (65 loc) · 1.85 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
65
66
67
68
69
70
71
72
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mwilliam <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:17:54 by mwilliam #+# #+# */
/* Updated: 2016/12/10 12:38:01 by mwilliam ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
** Returns a copy of the string without whitespaces at the beginning or end
*/
static int ft_isspacestrtrim(int c)
{
if (c == ' ' || c == '\n' || c == '\t')
return (1);
return (0);
}
static unsigned int ft_trimlength(char const *s)
{
size_t index;
size_t new_count;
size_t final_count;
index = 0;
new_count = 0;
final_count = 0;
while (ft_isspacestrtrim(s[index]))
index++;
while (s[index] != '\0')
{
index++;
new_count++;
}
while (ft_isspacestrtrim(s[--index]))
final_count++;
return (new_count - final_count);
}
char *ft_strtrim(char const *s)
{
char *copy;
size_t index;
size_t i;
index = 0;
i = 0;
if (!s)
return (NULL);
if (!(copy = (char *)malloc(sizeof(char) * ft_trimlength(s) + 1)))
return (NULL);
while (index < ft_strlen(s))
{
if (!i && ft_isspacestrtrim(s[index]))
index++;
else
{
copy[i++] = s[index++];
copy[i] = '\0';
}
}
while (ft_isspacestrtrim(copy[--i]))
copy[i] = '\0';
return (copy);
}