-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added function to free the array strings in ft_split
- Loading branch information
Matthew Yeow
committed
Apr 5, 2024
1 parent
6992714
commit f85edea
Showing
5 changed files
with
75 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: myeow <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/02/15 16:11:56 by myeow #+# #+# */ | ||
/* Updated: 2024/03/31 19:11:17 by myeow ### ########.fr */ | ||
/* Updated: 2024/04/05 19:04:15 by myeow ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -45,6 +45,7 @@ char *ft_substr(char const *s, unsigned int start, size_t len); | |
char *ft_strjoin(char const *s1, char const *s2); | ||
char *ft_strtrim(char const *s1, char const *set); | ||
char **ft_split(char const *s, char c); | ||
void ft_free_ft_split(char **str_array); | ||
char *ft_itoa(int n); | ||
char *ft_strmapi(char const *s, char (*f) (unsigned int, char)); | ||
void ft_striteri(char *s, void (*f) (unsigned int, char*)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_free_ft_split.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: myeow <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/04/05 18:59:36 by myeow #+# #+# */ | ||
/* Updated: 2024/04/05 19:02:20 by myeow ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include <stdlib.h> | ||
|
||
void ft_free_ft_split(char **str_array) | ||
{ | ||
int i; | ||
|
||
i = -1; | ||
while (str_array[++i]) | ||
free(str_array[i]); | ||
free(str_array); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: myeow <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/02/17 23:18:06 by myeow #+# #+# */ | ||
/* Updated: 2024/03/28 19:30:16 by myeow ### ########.fr */ | ||
/* Updated: 2024/04/05 20:08:21 by myeow ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -43,30 +43,66 @@ static char *ft_strdupsplit(char const *s, unsigned int start, unsigned int end) | |
return (str); | ||
} | ||
|
||
char **ft_split(char const *s, char c) | ||
static int ft_split_fail_malloc(char **array, char *array_str) | ||
{ | ||
if (array_str) | ||
return (0); | ||
else | ||
{ | ||
ft_free_ft_split(array); | ||
return (1); | ||
} | ||
} | ||
|
||
static char **ft_split_append(char const *s, char c, | ||
char **array, unsigned int i) | ||
{ | ||
char **array; | ||
unsigned int start; | ||
unsigned int i; | ||
size_t a; | ||
|
||
if (!s) | ||
return (0); | ||
array = (char **)malloc(sizeof(char *) * (ft_countwords(s, c) + 1)); | ||
if (!array) | ||
return (0); | ||
i = 0; | ||
while (s[i] == c && c) | ||
++i; | ||
start = i--; | ||
a = -1; | ||
while (s[++i]) | ||
{ | ||
if (s[i] != c && (s[i + 1] == c || !(s[i + 1]))) | ||
{ | ||
array[++a] = ft_strdupsplit(s, start, i); | ||
if (ft_split_fail_malloc(array, array[a])) | ||
return (0); | ||
} | ||
if (s[i] == c && s[i + 1] != c && s[i + 1]) | ||
start = i + 1; | ||
} | ||
array[++a] = 0; | ||
return (array); | ||
} | ||
|
||
char **ft_split(char const *s, char c) | ||
{ | ||
char **array; | ||
unsigned int i; | ||
|
||
if (!s) | ||
return (0); | ||
array = (char **)malloc(sizeof(char *) * (ft_countwords(s, c) + 1)); | ||
if (!array) | ||
return (0); | ||
i = 0; | ||
while (s[i] == c && c) | ||
++i; | ||
return (ft_split_append(s, c, array, i)); | ||
} | ||
/* | ||
#include <stdio.h> | ||
int main() | ||
{ | ||
char *str = "q q q q qq "; | ||
char **array_str = ft_split(str, ' '); | ||
int i = -1; | ||
while (array_str[++i]) | ||
printf("[%s]\n", array_str[i]); | ||
ft_free_ft_split(array_str); | ||
return (0); | ||
} | ||
*/ |