home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / string2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-29  |  1.4 KB  |  53 lines

  1. /*
  2.   C++ program that demonstrates comparing strings
  3. */
  4.  
  5. #include <iostream.h>                   
  6. #include <string.h>
  7.  
  8. const unsigned STR_SIZE = 40;
  9. const unsigned ARRAY_SIZE = 11;       
  10. const int TRUE = 1;
  11. const int FALSE = 0;
  12.  
  13. main()
  14. {
  15.  
  16.     char strArr[STR_SIZE][ARRAY_SIZE] =
  17.        { "California", "Virginia", "Alaska", "New York",
  18.          "Michigan", "Nevada", "Ohio", "Florida",
  19.          "Washington", "Orengon", "Arizona" };
  20.     char temp[STR_SIZE];
  21.     unsigned n = ARRAY_SIZE;
  22.     unsigned offset; 
  23.     int inOrder;
  24.        
  25.     cout << "Unordered array of strings is:\n";   
  26.     for (unsigned i = 0; i < ARRAY_SIZE; i++)
  27.       cout << strArr[i] << "\n";
  28.       
  29.     cout << "\nEnter a non-space character and press Enter";
  30.     cin >> temp[0];
  31.     cout << "\n";
  32.     
  33.     offset = n;
  34.     do { 
  35.       offset = (8 * offset) / 11;
  36.       offset = (offset == 0) ? 1 : offset;
  37.       inOrder = TRUE;
  38.       for (unsigned i = 0, j = offset;
  39.            i < n - offset; i++, j++)
  40.         if (strcmp(strArr[i], strArr[j]) > 0) {
  41.           strcpy(temp, strArr[i]);
  42.           strcpy(strArr[i], strArr[j]);
  43.           strcpy(strArr[j], temp);
  44.           inOrder = FALSE;
  45.         }
  46.     } while (!(offset == 1 && inOrder));      
  47.     
  48.     cout << "Sorted array of strings is:\n";   
  49.     for (i = 0; i < ARRAY_SIZE; i++)
  50.       cout << strArr[i] << "\n";
  51.     return 0;
  52. }
  53.