// basic_strings.cpp - partially based on examples from Deitel & Deitel

#include "mystrings.h"

void string_stats(const char *str_name, const string &str)
{
    cout << "\n" << str_name << ": " << str << endl;
    cout << "capacity: "   << str.capacity()
         << "\nmax size: " << str.max_size()
         << "\nsize: "     << str.size()
         << "\nlength: "   << str.length()
         << "\nempty: "    << (str.empty() ? "true": "false")
         << endl;
}

void basic_strings()
{
    string s1( "cat" ),
           s2 = "hello",
           s3;

    string_stats("s1", s1);
    string_stats("s2", s2);
    string_stats("s3", s3);

    cout << "\nAssigning s1 to s2 and s3\n";
    s2 = s1;
    s3.assign(s1);
    string_stats("s2", s2);
    string_stats("s3", s3);

    cout << "\nModifying s2 & s3 via indexing\n";
    s2[ 0 ] = s3[ 2 ] = 'r';
    cout << "s2: " << s2 << endl;
    cout << "s3: " << s3 << endl;            

    int len = s3.length();
    cout << "length of s3: " << len << endl;
    cout << "Outputing s3 character by character:\n";
    for (int i = 0; i < len; i++) 
        cout << s3[i];
    cout << endl;

    cout << "\nConcatenation via the + operator:\n";
    string s4( s1 + "apult" );  // "catapult"
    s1 += "acomb";              // "catacomb"
    cout << "s1: " << s1 << endl;
    cout << "s4: " << s4 << endl;

    cout << "3 char substring of s1 starting from index 4:\n";
    string s5 = s1.substr(4, 3);
    cout << "s5: " << s5 << endl;
    
    string s6;
    // append subscript locations 4 through the end of s1 to
    // create the string "comb" (s6 was initially empty)
    s6.append(s1, 4, s1.size());
    cout << "s6: " << s6 << endl;

    cout << "\nTesting string comparison:\n";
    if (s1 == s2)
    {
        cout << "s1 same as s2\n";
    }
    else
    {
        cout << "s1 different from s2\n";
    }

    if (s1 == "catacomb")
    {
        cout << "s1 is \"catacomb\"\n";
    }
    else
    {
        cout << "s1 is not \"catacomb\"\n";
    }

    cout << "length of s1: " << s1.length() << endl;
    cout << "\nAttempting to write & read s1[10] (out of range index):\n";
    s1[10] = 'e';
    cout << "s1[10]: " << s1[10] << endl;
    cout << "Note that the out-of-range error was not detected\n";
    cout << "Try again using the 'at()' function which does bounds checking\n";
    cout << "Attempting to read s1.at(10) (out of range index):\n";
    cout << "s1.at(10): " << s1.at(10) << endl;
}


