-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strreplace_first.c
47 lines (42 loc) · 1.72 KB
/
ft_strreplace_first.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
43
44
45
46
47
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strreplace_first.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: humbi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 19:42:33 by rbetz #+# #+# */
/* Updated: 2024/07/11 19:56:46 by humbi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *concat_parts(char *str, char *tofind, char *replace, int pos)
{
int str_len;
int tofind_len;
int replace_len;
int new_len;
char *newstr;
str_len = ft_strlen(str);
tofind_len = ft_strlen(tofind);
replace_len = ft_strlen(replace);
new_len = str_len - tofind_len + replace_len;
newstr = ft_calloc(new_len + 1, sizeof(char));
if (!newstr)
return (NULL);
ft_memcpy(newstr, str, pos);
ft_memcpy(newstr + pos, replace, replace_len);
ft_memcpy(newstr + pos + replace_len, str + pos + tofind_len,
str_len - pos - tofind_len + 1);
return (newstr);
}
char *ft_strreplace_first(char *str, char *tofind, char *replace)
{
char *searchstart;
int pos;
searchstart = ft_strstr(str, tofind);
if (!searchstart)
return (ft_strdup(str));
pos = searchstart - str;
return (concat_parts(str, tofind, replace, pos));
}