-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_last_word.c
More file actions
44 lines (41 loc) · 1.5 KB
/
Copy pathft_last_word.c
File metadata and controls
44 lines (41 loc) · 1.5 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_last_word.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbetz <rbetz@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/21 11:28:23 by rbetz #+# #+# */
/* Updated: 2023/02/22 11:06:50 by rbetz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*Returns a malloced string to last occurens of c in str from end*/
/*or if rev = 1 from start to the lim*/
char *ft_last_word(const char *str, char limiter, int rev)
{
int i;
int len;
char *ret;
if (str == NULL)
return (NULL);
len = ft_strlen(str);
i = len;
if (limiter == '\0')
limiter = ' ';
if (rev != 1)
rev = 0;
while (i >= 0 && str[i] != limiter)
i--;
if (rev == 0)
ret = ft_calloc(len - i + 1, sizeof(char));
else
ret = ft_calloc(i, sizeof(char));
if (ret == NULL)
return (NULL);
if (rev == 0)
ft_memcpy(ret, &str[i + 1], len - i);
else
ft_memcpy(ret, &str[0], i);
return (ret);
}