-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathft_strtrim.c
More file actions
51 lines (47 loc) · 1.51 KB
/
Copy pathft_strtrim.c
File metadata and controls
51 lines (47 loc) · 1.51 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* strtrim_main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jony <jony@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/30 10:44:11 by mhasan #+# #+# */
/* Updated: 2019/11/04 21:34:16 by jony ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s)
{
char *ret;
int ltrim;
int rtrim;
int len;
int i;
if (!s)
return (NULL);
len = ft_strlen(s);
ltrim = 0;
while (s[ltrim] == ' ' || s[ltrim] == '\n' || s[ltrim] == '\t')
ltrim++;
rtrim = len - 1;
while (s[rtrim] == ' ' || s[rtrim] == '\n' || s[rtrim] == '\t')
rtrim--;
len = rtrim - ltrim + 1;
if (len <= 0)
len = 0;
if (!(ret = malloc(sizeof(char) * (len + 1))))
return (NULL);
i = 0;
while (i < len)
ret[i++] = s[ltrim++];
ret[i] = '\0';
return (ret);
}
int main(void)
{
char *str = " ";
char *res;
res = ft_strtrim(str);
printf("%s", res);
return (0);
}