-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
43 lines (34 loc) · 917 Bytes
/
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
#include "libft.h"
short is_whitespace(char c)
{
if(c == ' ' || c == '\n' || c == '\t')
return 1;
return 0;
}
char * ft_strtrim(char *s)
{
int str_len;
str_len = ft_strlen(s);
char *s_end;
int trimmed_len;
char *trimmed_str;
// Subtract 1 to move it behind the null terminator
s_end = s + str_len - 1;
// Get start of trimmed string
while(is_whitespace(*s))
s++;
// Get end of trimmed string
while(is_whitespace(*s_end))
s_end--;
// Add 1 to make the length inclusive of both pointers
trimmed_len = s_end - s + 1;
// Add 1 to trimmed_len for null terminator in malloced string
trimmed_str = malloc(sizeof(char) * (trimmed_len+1));
if(trimmed_str == NULL)
{
return NULL;
}
ft_strncpy(trimmed_str, s, trimmed_len);
trimmed_str[trimmed_len] = '\0';
return trimmed_str;
}