Case-Insensitive String Sorting

  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 "Adam".
  4. Modify the cmpFunc() so that the sorting will be case-insentive. That is, it will ignore uppercase/lowercase so that "Adam" will be in front of "APPLE".