#Sorting function using pointers

19 messages · Page 1 of 1 (latest)

jagged harness
#

Given the following code, why does the last function sortAccordingToFunction sometimes omit the last word or puts a comma at the beginning? I am limited to using cstring

int compareTwoWordsLowerAndCh(const void *a, const void *b) {
    const char *first = *(const char *const *) a;
    const char *second = *(const char *const *) b;

    char lowerFirst[strlen(first)];
    char lowerSecond[strlen(second)];

    strcpy(lowerFirst, first);
    strcpy(lowerSecond, second);

    toLower(lowerFirst);
    toLower(lowerSecond);

    if (strncmp(lowerFirst, "ch", 2) == 0) {
        if (strncmp(lowerSecond, "ch", 2) == 0) {
            return strcmp(lowerFirst, lowerSecond);
        }
        if (lowerSecond[0] <= 'h') {
            return 1;
        }
        return -1;
    }
    if (strncmp(lowerSecond, "ch", 2) == 0) {
        if (lowerFirst[0] <= 'h') {
            return -1;
        }
        return 1;
    }
    return strcmp(lowerFirst, lowerSecond);
}

void sortWordsInSmallAndCh(char *listOfWords[], int count) {
    qsort(listOfWords, count, sizeof(char *), compareTwoWordsLowerAndCh);
}

const char *sortAccordingToFunction(const char *text, char *output, void sortingFunction(char *arraysOfText[], int count)) {
    if (text == nullptr || output == nullptr || numberOfWords(text) == 0) {
        return "";
    }
    char **buffer = new char *[numberOfWords(text)];
    for (int i = 0; i < numberOfWords(text); i++) {
        buffer[i] = new char[256];
        copyNthWord(text, buffer[i], i);
    }
    sortingFunction(buffer, numberOfWords(text));
    memset(output, 0, strlen(output));
    bool needComma = false;
    for (int i = 0; i < numberOfWords(text); i++) {
        if (needComma) {
            strcat(output, ",");
        }
        strcat(output, buffer[i]);
        needComma = true;
    }

    for (int i = 0; i < numberOfWords(text); i++) {
        delete[] buffer[i];
    }
    delete[] buffer;

    return output;
}
severe yarrowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

jagged harness
#

For example the output for

char *output = new char[256];
sortAccordingToFunction("Cpp,aaa,Vvvv,gggg,Hhhh,to,Chccc,zz", output, sortWordsInSmallAndCh);

is

"aaa,Cpp,gggg,Hhhh,Chccc,to,Vvvv,"

instead of

"aaa,Cpp,gggg,Hhhh,Chccc,to,Vvvv,zz"

I can provide code for the other functions used, as needed.

timid dagger
#

Did you try debugging?

jagged harness
timid dagger
#

What did you observe?

severe yarrowBOT
#

@jagged harness Has your question been resolved? If so, run !solved :)

jagged harness
timid dagger
#

Debugging should have made it certain.

jagged harness
timid dagger
#

Well the output says otherwise.

#

If it was correct then you would have gotten the correct output.

jagged harness
timid dagger
#

That adds nothing, again you need a certainty.

jagged harness
#

I am certain

#

on the first iteration it doesn't store anything and, as a result, omits the last word

timid dagger
#

Well you know what to fix then.

jagged harness
#

!solved

severe yarrowBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity