-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
64 lines (59 loc) · 1.63 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: hshawand <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/03 15:19:14 by hshawand #+# #+# */
/* Updated: 2019/04/15 12:57:57 by hshawand ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <string.h>
static size_t ft_memory_count(char const *s)
{
size_t count;
count = 0;
if (!(*s))
return (0);
while (*s == ' ' || *s == '\t' || *s == '\n')
s = s + 1;
while (*s)
{
count++;
s = s + 1;
}
count++;
while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\0')
{
if (count == 0)
return (0);
count--;
s = s - 1;
}
return (count);
}
char *ft_strtrim(char const *s)
{
size_t size;
char *trim;
size_t i;
if (!s)
return (NULL);
size = ft_memory_count(s);
i = 0;
trim = (char *)malloc(sizeof(char) * (size + 1));
if (!trim)
return (NULL);
while (*s == ' ' || *s == '\t' || *s == '\n')
s = s + 1;
while (i < size)
{
trim[i] = *s;
s = s + 1;
i++;
}
trim[i] = '\0';
return (trim);
}