#include<iostream.h>

// to bring in string library (strtok, strcmp)
#include<strings.h>

// SmallestWord.C
// Introduces the string library, input of strings,
//  pointers and strings.


void main()
{
   const int BUFFER_N = 200 ;
   char buffer[BUFFER_N] ;
   char * smallest ;
   char * s ;

   cout << "This program takes a line of words and finds the smallest.\n" ;
   cout << "Input: " ;

   cin.getline( buffer, BUFFER_N ) ;

   s = strtok( buffer, " \t\n" ) ;
   // s points to first non-delimiter char.
   if ( s==NULL ) 
   {
      // nothing to do
      cout << "Input string contains no words.\n" ;
      return ;
   }

   smallest = s ;
   s = strtok( NULL, " \t\n" ) ;
   // LOOP INVARIANT: smallest points to the smallest work in buffer
   //  up to, not including word pointed to by s.
   while ( s!=NULL )
   {
      if ( strcmp( s, smallest ) < 0 )
      {
         // if word pointed to by s is smaller ...
         smallest = s ;
      }
      // next word to check
      s = strtok( NULL, " \t\n" ) ;
   }
   // Invariant + Termination => Goal, smallest is smallest word in buffer.

   cout << "Smallest: " << smallest << endl ;

}

