I am trying to create a ft_split function which should:
Allocates with malloc and returns an array of strings obtained by splitting ’s’ using the character ’c’ as a delimiter. The array must end with a NULL pointer.
I have included all the code with the code sample below and the breakpoint where the debugger is giving me one of the errors.
now am having problems with:
- when I input a string and at the end of it i add the delimiter it gives this error
the string:
" To be or not to be that is the question "
the error:
Segmentation fault
- when I input this string
the string:
"Hello there"
it will give the right answer and then this error
Hello
there
Segmentation fault
- i just cant figure out how i should free the memory allocations created i have included what i think was the correct way commented.
my code:
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i] != '\0')
{
i++;
}
return (i);
}
char *ft_strcpy(char *dest, const char *src)
{
int i;
i = 0;
while (src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}
char *string_length(char const *str, char c, int i)
{
int j;
char *string;
j = 0;
// when i try the debugger with the first string in the main function it goes
// directly to this line and break.
string = (char *)malloc((ft_strlen(str)) * sizeof(*string));
if(!string)
return (NULL);
while (str[i] == c)
i++;
while (str[i])
{
*string = str[i];
if (str[i + 1] == c || str[i + 1] == '\0')
{
string++;
j++;
*string = '\0';
return (string - j);
}
string++;
j++;
i++;
}
return (0);
}
int count_strings(char const *str, char c)
{
int i;
int count;
int def;
i = 0;
count = 0;
def = 1;
while (str[i] == c)
i++;
while (str[i])
{
if (str[i] == c && def == 1)
{
i++;
def = 0;
}
if (str[i] != c && def == 0)
{
count++;
def = 1;
}
i++;
}
return (count);
}
int first_del(char const *str, char c, int i)
{
while (str[i] == c)
i++;
return (i);
}
char **ft_split(char const *s, char c)
{
int count;
int i;
int len;
int num_of_strings;
char **split;
char *string;
char **it;
i = 0;
count = 0;
num_of_strings = count_strings(s, c);
split = (char **)malloc((num_of_strings + 1) * sizeof(char *));
if (!split)
return (NULL);
split[num_of_strings + 1] = NULL;
while (i <= num_of_strings)
{
count = first_del(s, c, count);
string = string_length(s, c, count);
len = ft_strlen(string);
split[i] = (char *)malloc(len + 1);
ft_strcpy(split[i], string);
count += len;
i++;
free(string);
string = NULL;
}
return (split);
}
int main(void)
{
char s[] = " To be or not to be that is the question ";
char **split_strings = ft_split(s, ' ');
for (int i = 0; i < 10; i++)
printf("%s\n", split_strings[i]);
char s2[] = "Hello there";
char **split_strings2 = ft_split(s2, ' ');
for (int i = 0; i < 2; i++)
printf("%s\n", split_strings2[i]);
}