
/********************************************************************/
/*  ANSIString.cpp  -  Demonstrate the use of the ANSI strings      */
/*  Author:            Bary W Pollack                               */
/*  Last Update:       07-21-2001                                   */
/********************************************************************/

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

/********************************************************************/

int main (void)
{
    cout << endl << "Demonstrating ANSI strings..." << endl << endl;

    cout << "You can declare strings just like any other basic type..." << endl;
    string sSchool, sCity ("Incline Village"), sState ("N");

    cout << "Assignment and initialization work properly..." << endl;
    sSchool = "Sierra Nevada College";
    sState = sState + "evada";

    cout << "Of course I/O works just fine..." << endl;
    cout << "School:  " << sSchool << endl
         << "City:    " << sCity   << endl
         << "State:   " << sState  << endl << endl;

    cout << "More importantly, you've got assignment and concatenation" << endl;
    string sC1 = "Computer ",
           sC2 = "Science / ",
           sC3 = "Science Department";
    string sCombined = sC1 + sC2 + sC3;
    cout << "sCombined:  " << sCombined << endl << endl;

    cout << "Memory for strings is handled automatically..." << endl;
    sCombined = "PRESTIGIOUS ";
    cout << "sCombined:  " << sCombined << " -- no lost memory" << endl << endl;

    cout << "And, += appends -- as one would expect..." << endl;
    sCombined += sC3;
    cout << "sCombined:  " << sCombined << endl << endl;

    cout << "Of course, the < <= == != >= > operators all work..." << endl;
    cout << sC1 << ((sC1 < sC3) ? "< " : ">= ") << sC3 << endl << endl;

    cout << "Insert one string at a position in another string..." << endl;
    sC1 = "abcdefghijklmnopqrstuvwxyz";
    sC1.insert (5, "12345");
    cout << "Result is:  " << sC1 << endl << endl;

    cout << "Erase is around as well..." << endl;
    sC1.erase (5, 21);
    cout << "Result is:  " << sC1 << endl << endl;

    cout << "Replace, too..." << endl;
    sC1.replace (2, 4, "TEST");
    cout << "Result is:  " << sC1 << endl << endl;

    cout << "Subscription works, as expected..." << endl;
    cout << sC1 [0] << " " << sC1 [1] << endl << endl;

    cout << "and substring(2,4)..." << endl;
    cout << sC1.substr (2, 4) << endl << endl;

    cout << "Of course, there's length()..." << endl;
    cout << "The length of (" << sC1 << ") is " << sC1.length() << endl << endl;

    cout << "Convert from a 'string' to a C-string..." << endl;
    char sCstring [64];
    strcpy (sCstring, sC1.c_str());
    cout << "sCstring is (" << sCstring << ")" << endl;

    cout << endl;
    return 0;
}

/********************************************************************/

