0

I have a homework problem I'm having bit of a problem with, I'm asked to sort an array of C strings alphabetically using C++, sorting algo used must be bubble sort. What I've done so-far (replicated below) can sort the array but only based on the first alphabet. How do I further sort strings the strings with the same initial alphabet ?

<snipped>@arch:~/College/OOP/Lab/W3$ cat 2.cpp

/*
 * Write a function which sorts an array of C strings in ascending order using bubble sort. The
 * number of strings in the array and the array must be passed as parameters to the function
 */

#include <iostream>
#include <cstring>

using namespace std;

void sort(char **sar, unsigned num, unsigned len)
{
    char *temp = new char[len];

    if (temp == NULL)
    {
        cout << "\nOut-Of-Memory\n";
        return;
    }

    for (unsigned a = 0; a < num-1; a++)
    {
        for (unsigned b = 0; b < ((num-a)-1); b++)
        {
            if (sar[b][0] > sar[b+1][0])
            {
                strcpy(temp, sar[b]);
                strcpy(sar[b], sar[b+1]);
                strcpy(sar[b+1], temp);
            }
        }
    }

    delete[] temp;
}

int main(int argc, char *argv[])
{
    char **sar;
    unsigned num;
    unsigned len;

    cout << "Number of Strings: ";
    cin  >> num;
    cout << "Length of Strings: ";
    cin  >> len;

    cin.ignore(); // Flush buffer to fix a bug (getline after cin).

    sar = (char **) new char*[num];
    if (sar == NULL)
    {
        cout << "\nOut-Of-Memory\n";
        return -1;
    }

    for (unsigned i = 0; i < num; i++)
    {
        sar[i] = (char *) new char[len];
        if (sar[i] == NULL)
        {
            // Let's pretend we 'know' memory management
            // because obviously modern OSs are incapable
            // of reclaiming heap from a quitting process..
            for (unsigned j = 0; j < i; j++)
                delete[] sar[j];
            cout << "\nOut-Of-Memory\n";
            return -1;
        }
    }

    for (unsigned x = 0; x < num; x++)
        cin.getline(&sar[x][0], 512);

    sort(sar, num, len);

    cout << '\n';
    for (unsigned y = 0; y < num; y++)
        cout << sar[y] << '\n';

    for (unsigned z = 0; z < num; z++)
        delete[] sar[z];
    delete[] sar;

    return 0;
}
4
  • Invalid use of delete. You should be using delete[]. Commented Feb 9, 2014 at 22:04
  • where exactly ? valgrind doesn't grin about anything as such. Commented Feb 9, 2014 at 22:04
  • In your sort function as well. Near the end, you call delete on temp.. It should be delete[]. Commented Feb 9, 2014 at 22:05
  • thanks but that doesn't really make a difference to my problem. Will update in a jiffy. Commented Feb 9, 2014 at 22:06

1 Answer 1

1

change

if (sar[b][0] > sar[b+1][0])

to

if (stricmp(sar[b], sar[b+1]) > 0)

UPDATE: instead of stricmp, you can use strcasecmp

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

2 Comments

That's not standard, is it? (also see here)
Thanks a lot! I searched a bit about the function and found an equivalent strcasecmp in g++ manual which does the trick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.