-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrunc.c
35 lines (32 loc) · 1.36 KB
/
ft_strtrunc.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrunc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dbaldy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/02/10 12:15:58 by dbaldy #+# #+# */
/* Updated: 2016/02/13 12:46:57 by dbaldy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrunc(char *str, int c)
{
char *ret;
int trunc;
char *buf;
if ((buf = (char*)malloc(sizeof(char) * 2)) == NULL)
exit(1);
buf[0] = (char)c;
buf[1] = '\0';
if (ft_strcmp(str, buf) == 0)
{
free(buf);
return (ft_strdup(str));
}
free(buf);
trunc = (ft_strchr(str, c) != NULL) ? ft_strlen(ft_strrchr(str, c)) : 0;
trunc = ((size_t)trunc == ft_strlen(str)) ? trunc - 1 : trunc;
ret = ft_strsub(str, 0, ft_strlen(str) - trunc);
return (ret);
}