home *** CD-ROM | disk | FTP | other *** search
- /*
- C++ program that demonstrates comparing strings
- */
-
- #include <iostream.h>
- #include <string.h>
-
- const unsigned STR_SIZE = 40;
- const unsigned ARRAY_SIZE = 11;
- const int TRUE = 1;
- const int FALSE = 0;
-
- main()
- {
-
- char strArr[STR_SIZE][ARRAY_SIZE] =
- { "California", "Virginia", "Alaska", "New York",
- "Michigan", "Nevada", "Ohio", "Florida",
- "Washington", "Orengon", "Arizona" };
- char temp[STR_SIZE];
- unsigned n = ARRAY_SIZE;
- unsigned offset;
- int inOrder;
-
- cout << "Unordered array of strings is:\n";
- for (unsigned i = 0; i < ARRAY_SIZE; i++)
- cout << strArr[i] << "\n";
-
- cout << "\nEnter a non-space character and press Enter";
- cin >> temp[0];
- cout << "\n";
-
- offset = n;
- do {
- offset = (8 * offset) / 11;
- offset = (offset == 0) ? 1 : offset;
- inOrder = TRUE;
- for (unsigned i = 0, j = offset;
- i < n - offset; i++, j++)
- if (strcmp(strArr[i], strArr[j]) > 0) {
- strcpy(temp, strArr[i]);
- strcpy(strArr[i], strArr[j]);
- strcpy(strArr[j], temp);
- inOrder = FALSE;
- }
- } while (!(offset == 1 && inOrder));
-
- cout << "Sorted array of strings is:\n";
- for (i = 0; i < ARRAY_SIZE; i++)
- cout << strArr[i] << "\n";
- return 0;
- }
-