/* name3.cpp driver program to tests the function DecomposeName().
 *
 * Input:  a person's full name
 * Output: the person's first name, middle name, and last name.
 ******************************************************************/

#include <iostream>                // cout, cin, <<, >>
#include <string>                  // string, getline

void DecomposeName(const string & fullName, string & firstName,
                   string & middleName, string & lastName);

int main()
{
   cout << "Enter a full name: ";
   string fullName;
   getline(cin, fullName);

   string fName, mName, lName;
   DecomposeName(fullName, fName, mName, lName);

   cout << fName << endl
        << mName << endl
        << lName << endl;

   return 0;
}

/* DecomposeName breaks down a full name into its 3 parts.
 *
 *  Receive:      fullName, a string
 *  Precondition: fullName contains 3 names separated by blanks
 *  Pass back:    the 3 parts: firstName, middleName and lastName
 *****************************************************************/

#include <cassert>        // assert()

void DecomposeName(const string & fullName, string & firstName,
                   string & middleName, string & lastName)
{
   int firstBlankIndex = fullName.find(' ', 0);
   assert(firstBlankIndex != NPOS);
   firstName = fullName.substr(0, firstBlankIndex);

   int secondBlankIndex = fullName.find(' ', firstBlankIndex + 1);
   assert(secondBlankIndex != NPOS);
   middleName = fullName.substr(firstBlankIndex + 1, 
                                 secondBlankIndex - firstBlankIndex - 1);

   int fullNameSize = fullName.size();
   lastName = fullName.substr(secondBlankIndex + 1, 
                               fullNameSize - secondBlankIndex - 1);
}


