0

I've been trying to create a function that splits a string and return a pointer to the first element of the array. It compiles with no error but when i run the program it crashes. Here is my code. Any help on how to fix this. Thanks.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define split_count(a) a

int count(char *str, char *sub) {
    int sublen = strlen(sub);
    int templen = 0;
    int count = 0;

    if (sublen > strlen(str))
        return 0;

    int i, j;
    for (i = 0; i < strlen(str); i++) {
        if (*(str + i) == *sub) {
            templen = 1;
            for (j = 1; j < sublen; j++) {
                if (*(str + i + j) == *(sub + j)) {
                    templen += 1;
                }
            }

            if (templen == sublen) {
                count += 1;
            }

            templen = 0;
        }
    }

    return count;
}

char * split(char *str, char *sep, int maxsplit) {
    if (!count(str, sep))
        return NULL;

    char *arr[split_count(count(str, sep)) + 1];

    int i, j;
    int templen = 0;

    int stop = 0;

    int counter = 0;

    for (i = 0; i < strlen(str); i++) {
        if (*(str + i) == *sep) {
            templen = 1;

            for (j = 1; j < strlen(sep); j++) {
                if (*(str + i + j) == *(sep + j)) {
                    templen += 1;
                }

                if (templen == strlen(sep)) {
                    arr[counter] = (char*)malloc(sizeof(char) * strlen(str));

                    strcpy(arr[counter], "");

                    int k;
                    for (k = stop; k < i; k++) {
                        *(arr[counter] + strlen(arr[counter])) = *(str + k);
                        *(arr[counter] + strlen(arr[counter])) = '\0';
                    }

                    stop = i + strlen(sep);

                    counter++;
                }
            }
        }
    }

    return arr[0];
}

int main() {
    char *before = "This is a house isisis is";

    printf("%s\n", split(before, "is", 1));

    return 0;
}
2
  • There is no way this would compile without warnings. You should turn compilation warnings on. Commented Oct 15, 2013 at 10:38
  • And you should avoid calling strlen() repeatedly. As str doesn't change its length, it would be better to determine it once and use that value. That saves time. Commented Oct 15, 2013 at 11:43

2 Answers 2

1

In addition to Acme's answer that you may not modify a string literal:

Looks like your looping through str, increasing i up to i < strlen(str), however, below that you go to str+i+j. What happens when j is like 10 and you are at the the very end of str? You most likely end up going outside of the memory are for str resulting in a segmentation fault.

I recommend looking through string.h which you already include. There are functions for doing this in a much safer way.

Sign up to request clarification or add additional context in comments.

Comments

1

This

char *before = "This is a house isisis is";

is a pointer to string literal "This is a house isisis is" - modifying it will crash the program.

Use this:

char before[50] = "This is a house isisis is";

Also your split function has an infinite loop.

1 Comment

"modifying it will crash the program" ~ it doesn't necessarily has to crash, but it's in read-only memory and trying to modify it yields UB, yeah.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.