// Program to read three strings and determine which comes first in
// alphabetical order

// Notes: 
//    1.  C++ includes two approaches for using strings, one based on
//        strings in C and one based on C++ classes
//    2.  Within C++, C-style strings may be used with #include <cstring>
//        while a class-based approach may be used with #include <>
//    3.  The what follows demonstates both approaches for the MathLAN compiler

#include <iostream.h>

// C-style string library:
#include <cstring>
// representation
//   a string are stored as an array of char
//   a null character follows the last logical string character
//   individual characters may be referenced using appropriate array subscripts
// common operations 
//        where c is a character, 
//              cs and ct are strings that remain unchanged,
//              s and t are strings that are changed by the operation,
//              n is an integer
//   >>         input from stream
//   <<         output to stream
//   strlen (cs)      returns integer length
//   strcpy (s, ct)   copies ct to s and also returns s
//   strcat (s, ct)   concatenates ct to end of s and also returns s
//   strcmp (cs, ct)  compares string cs to string ct
//                       returns value < 0 if cs<cs,
//                       returns  0 if cs==cs,
//                       returns value > 0 if cs>cs
//   strchr (cs, c)   returns address of first occurrance of c in cs,
//                       or NULL if c is not present
//   strrchr (cs, c)  returns address of last occurrance of c in cs,
//                       or NULL if c is not present
//   strstr (cs, ct)  returns address for first occurrance of ct in cs 
//                       or NULL if ct is not present
//   strspn (cs, ct)  returns the length of the prefix of cs consisting of
//                       characters in ct

// C++'s string class definition:
#include <string>
// representation
//   an object of class string is a self contained entity
// common methods and operations
//   >>         input from stream
//   <<         output to stream
//   size()       returns integer length
//   empty ()     returns "true" if string is empty, otherwise returns false
//   +            concatenates strings
//   c_str        return a C-style representation (array) of this string
//   ==, <=, etc. alphabetical comparisons
//   [ ]          characters within string may be accessed by index
//   =            assignment/copy operator
//   

int main (void) {
  cout << "Program to determine which of 3 strings comes first" << endl;

   // declarations and processing for C-style strings, using <cstring>
   cout << endl << "Processing using C-style strings" << endl;
   const int MAX = 20;  /* maximum size of a string */
   char s1[MAX];
   char s2[MAX];
   char s3[MAX];
   char smaller[MAX];

   cout << "Enter first string: ";
   cin >> s1;
   cout << "Enter second string: ";
   cin >> s2;
   cout << "Enter third string: ";
   cin >> s3;

   if (strcmp(s1, s2) <= 0)
      strcpy (smaller, s1);
   else strcpy (smaller, s2);

   if (strcmp(smaller, s3)  <= 0)
     cout << smaller << " comes first." << endl;
   else  cout << s3 << " comes first." << endl;

   cout << "The first letter of the third string is " << s3[0] << endl;
   cout << "The third string contains " << strlen(s3) << " letters." << endl;


   // declarations and processing for C++'s string class, using <string>
   cout << endl << "Processing with C++'s string class" << endl;
   string st1(s1);  /* declare object st1, initialized by C-string s1 */
   string st2(s2);
   string st3;      /* string object -- by default set to null string */
   string small;

   cout << "Enter third string again: ";
   cin >> st3;

   if (st1 <= st2)
     small = st1;
   else small = st2;

   if (small < st3)
     cout << small << " comes first." << endl;
   else  cout << st3 << " comes first." << endl;

   cout << "The first letter of the third string is " << st3[0] << endl;
   cout << "The third string contains " << st3.size() << " letters." << endl;

   
}
