Sorting strings reversely by qsort()

  1. Read the manpage of qsort().
  2. Consider the following program, which will read N words and sort them in alphabetical order.
    
    #include <iostream>
    using std::cin;
    using std::cout;
    
    int cmpFunc( const void *p1, const void * p2)
    {
        const char* str1 = *(const char **)(p1);
        const char* str2 = *(const char **)(p2);
        return strcmp(str1, str2);
    }
    
    void show_array(char* a[], int n)
    {
        int i;
        for (i=0; i<n; i++)
            cout << a[i] << '\n';
    }
    
    int main()
    {
        const int N = 10;
        char word[N][20];
        char* a[N];
    
        for (int i=0; i<N; i++)
        {
            cin >> word[i];
            a[i] = &word[i][0];
        }
    
        qsort(a, N, sizeof(a[0]), cmpFunc);
        show_array(a, N);
        return 0;
    }
    
    
  3. Note the words are sorted by ASCII code, so uppercase alphabets will precede lowercase alphabets. For example, "APPLE" will be in front of "BANANA", but "BANANA" will be infront of "apple".
  4. Modify the cmpFunc() so that the sorting will be in reverse order. That is, "BANANA" will be in front of "APPLE".