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;
}