Home > Mobile >  Keep getting segfault when trying to split a sentence into an array of arrays
Keep getting segfault when trying to split a sentence into an array of arrays

Time:11-24

I've only been learning programming for the last 2 months and it's my first time using StackOverflow, so hopefully I didn't screw up formatting. I've been stuck on this exercise for a few days.

Description:

Parameters: #1. The string to be split. #2. The delimiter character.

Return value: The array of new strings resulting from the split. NULL if the allocation fails.

External functions allowed: malloc, free

Allocates (with malloc(3)) and returns an array of strings obtained by splitting ’s’ using the character ’c’ as a delimiter. The array must be ended by a NULL pointer.

I know surely there's an easier way to do it, but I'm extremely fearful of searching the web because I feel like I'm cheating myself out of learning by trying. What I decided to do was creating a counter function that checks how many words will be between the delimiter (aka size of first array), then I check the size of each word and store that in an array of ints that I use to create the length of each array to which the first array will point to. Then I fill each array.

The tester I use says I've segmentation faults in two of the 5 tests, even though the output from my function matches the supposed output of the test.

I've been really stuck and starting to feel really bad for being behind my friends who already turned in the entire project.

Hopefully someone can teach me what I'm doing wrong. Thank you for your time!

    char    **ft_split(char const *s, char c)
{
    int     i;
    int     size;
    int     *elem;
    char    **new;

    size = counter(s, c);
    elem = sizeofeachstring(s, c);
    i = 0;
    if (s == NULL)
        return (NULL);
    new = malloc(sizeof(char *) * size);
    if (new == NULL)
        return (NULL);
    while (i < size)
    {
        if (i < size)
            new[i] = (char *)malloc(sizeof(char) * (elem[i]   1));
        i  ;
    }
    fillarrays(s, c, new);
    return (new);
}

static int      counter(char const *s, char c)
{
    int     count;
    int     i;

    i = 0;
    count = 0;
    while (s[i])
    {
        while (s[i] == c)
            i  ;
        if (s[i] != c && s[i])
            count  ;
        while (s[i] != c && s[i])
            i  ;
    }
    return (count);
}

static int  *sizeofeachstring(char const *s, char c)
{
    int i;
    int len;
    int n;
    int *elem;
    int size;

    i = 0;
    len = 0;
    n = 0;
    size = counter(s, c);
    elem = (int *)malloc(sizeof(int) * size);
    while (s[i] != '\0')
    {
        if (s[i] != c)
            len  ;
        if ((s[i] != c && s[i   1] == c && len > 0))
        {
            elem[n] = len;
            len = 0;
            n  ;
        }
        i  ;
    }
    return (elem);
}

static char **fillarrays(char const *s, char c, char **new)
{
    int i;
    int j;
    int k;

    j = 0;
    i = 0;
    k = 0;
    while (s[i] != '\0')
    {
        while (s[i] == c)
            i  ;
        if (s[i] == '\0')
            break ;
        if (s[i] != c)
        {
            new[j][k  ] = s[i  ];
            if (s[i] == c)
            {
                new[j][k] = '\0';
                if (s[i] != '\0' || s[i   1] != '\0')
                {
                    j  ;
                    k = 0;
                }
            }
        }
    }
    return (new);
}

CodePudding user response:

try to fill the **new with NULL after fill the words in array

char    **ft_split(char const *s, char c)
{
...
   new = malloc(sizeof(char *) * (size   1));
...
}
static char **fillarrays(char const *s, char c, char **new)
{
...
   new[j] = NULL;
   return (new)
}

from the name of your function i think you are studying at one of 42Network schools additional thing you need to protect the malloc that's a norm error

  • Related