-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strreplace.c
36 lines (33 loc) · 1.69 KB
/
ft_strreplace.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strreplace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: humbi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/18 15:32:03 by rbetz #+# #+# */
/* Updated: 2024/07/11 19:56:42 by humbi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
// This function replaces the first occurrence of 'tofind' with 'replace'
// in 'str'. It modifies the original string 'str' in place. Note: This
// implementation is not safe if 'replace' is longer than 'tofind', as
// it does not allocate additional memory to accommodate a longer
// replacement string. Use ft_strreplace_first instead.
void ft_strreplace(char *str, char *tofind, char *replace)
{
char *tempstring;
char *searchstart;
int len;
len = 0;
searchstart = ft_strstr(str, tofind);
tempstring = ft_strdup(str);
len = ft_strlen(str) - ft_strlen(searchstart);
str[len] = '\0';
ft_strlcat(str, replace, ft_strlen(str) + ft_strlen(replace) + 1);
len += ft_strlen(tofind);
ft_strlcat(str, &tempstring[len], ft_strlen(tempstring) \
- ft_strlen(tofind) + ft_strlen(replace) + 1);
free(tempstring);
}