aboutsummaryrefslogtreecommitdiff
path: root/ft_strtrim.c
diff options
context:
space:
mode:
Diffstat (limited to 'ft_strtrim.c')
-rw-r--r--ft_strtrim.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/ft_strtrim.c b/ft_strtrim.c
new file mode 100644
index 0000000..7b93eae
--- /dev/null
+++ b/ft_strtrim.c
@@ -0,0 +1,27 @@
+#include <stdlib.h>
+#include "libft.h"
+
+static int is_space(char c)
+{
+ return (c == ' ' || c == '\n' || c == '\t');
+}
+
+char *ft_strtrim(char const *s)
+{
+ size_t start;
+ size_t len;
+ char *trimed;
+
+ start = 0;
+ while (s[start] && is_space(s[start]))
+ start++;
+ len = ft_strlen(&s[start]);
+ if (len != 0)
+ while (s[start + len - 1] && is_space(s[start + len - 1]))
+ len--;
+ if ((trimed = (char*)malloc(sizeof(char) * (len + 1))) == NULL)
+ return (NULL);
+ trimed = ft_strncpy(trimed, &s[start], len);
+ trimed[len] = '\0';
+ return (trimed);
+}