I keep getting a segfault every time I run this program, but I cannot understand why

Try something more like this:

int indexOf(const char * sequence, char ch) {
    const char *p = sequence;
    while (*p != '\0') {
        if (*p == ch) {
            return p - sequence;
        }
    }
    return -1;
}

const char* findFirstOf(const char * sequence, const char *chars) {
    const char *p = sequence;
    while (*p != '\0') {
        if (indexOf(chars, *p) != -1)  {
            return p;
        }
    }
    return NULL;
}

const char* findFirstNotOf(const char * sequence, const char *chars) {
    const char *p = sequence;
    while (*p != '\0') {
        if (indexOf(chars, *p) == -1)  {
            return p;
        }
    }
    return NULL;
}

int countWords(const char * sequence, char * delims) {
    int count = 0;
    const char *p = sequence;
    do {
        p = findFirstNotOf(p, delims);
        if (p == NULL) break;
        ++count;
        p = findFirstOf(p, delims);
    }
    while (p != NULL);
    return count;
}

int getLength(const char * sequence) {
    const char *p = sequence;
    while (*p != '\0') {
        ++p;
    }
    return p-sequence;
}

char* dupString(const char * sequence, int length = -1) {
    if (length == -1) {
        length = getLength(sequence);
    }
    char *result = new char[length+1];
    for (int i = 0; i < length; ++i) {
        result[i] = sequence[i];
    }
    result[length] = '\0';
    return result;
}

char** getWords(const char * sequence, int & wordcount) {
    const char delims[] = " .,\t?!";
    int count = countWords(sequence, delims);
    char ** words = new char *[count];
    if (count > 0) {
        count = 0;
        const char *p = sequence;
        do {
            p = findFirstNotOf(p, delims);
            if (p == NULL) break;
            const char *q = findFirstOf(p, delims);
            if (q == NULL) {
                words[count++] = dupString(p);
                break;
            }
            words[count++] = dupString(p, q-p);
            p = ++q;
        }
        while (true);
    }
    wordcount = count;
    return words;
}

That being said, the fact you are using new[] means you are using C++, so you should be using the STL to make life easier:

#include <string>
#include <vector>

std::vector<std::string> getWords(const std::string & sequence) {
    const char delims[] = " .,\t?!";
    std::vector<std::string> words;
    std::string::size_type i = 0;
    do {
        i = sequence.find_first_not_of(delims, i);
        if (i == std::string::npos) break;
        std::string::size_type j = sequence.find_first_of(delims, i);
        if (j == std::string::npos) {
            words.push_back(sequence.substr(i));
            break;
        }
        words.push_back(sequence.substr(i, j-i));
        i = ++j;
    }
    while (true);
    return words;
}

Leave a Comment