Now here is a little test program to start to test out the class MyString ...
// testing_MyString.cpp // // 2016-02-05 //
// a demo of loops, arrays of char, new and delete (for arrays of char)
// recall that C strings ARE arrays of char terminated by a 'NULL char'
// i.e. arrays of char ... terminated by '\0'
#include "MyString.h"
#include <fstream>
#include <iomanip> // re. precision ...
////////////////////////////////////////////////////////////
// only used here to see AND to compare
// sizeof C++ string object
// with ...
// sizeof MyString object
#include <string>
using std::string;
////////////////////////////////////////////////////////////
const char* FNAME = "words.txt";
// just a few words as per below to test reading from file //
/*
sam joe bill harry ann jill bonnie
*/
int main()
{
using std::cout;
using std::endl;
using std::vector;
const size_t num_bytes = sizeof(size_t);
cout << "Number of bytes in type size_t is " << num_bytes << '\n';
// form the largest number that fits into a memory block with size of type size_t ...
size_t t = 1;
for( size_t i = 1; i < num_bytes*8; ++ i )
t = (t << 1) + 1; // shift left 1 byte and then add in 1 //
cout << "Largest size_t = " << commasAdded( t ) << '\n';
cout << "MyString::npos = " << commasAdded( MyString::npos ) << "\n\n";
cout << "sizeof(MyString) = " << sizeof(MyString) << '\n'
<< "sizeof(MyString*) = " << sizeof(MyString*) << '\n'
<< "sizeof(string) = " << sizeof(string) << '\n'
<< "sizeof(string*) = " << sizeof(string*)
<< "\n\n";
cout << "\nTesting toNumber(12345.67890) ...\n";
cout << std::setprecision(16);
double nVal = toNumber< double >( "12345.67890" );
cout << nVal<< "\n\n";
MyString s1, s2(" is working very diligently at U!" );
cout << "'" << s1 << "'" << " has len " << s1.size() << '\n';
cout << "'" << s2 << "'" << " has len " << s2.size() << '\n';
s1 = "Sam";
cout << "'" << s1 << "'" << " has len " << s1.size() << '\n';
s1 += s2;
cout << "'" << s1 << "'" << " has len " << s1.size() << '\n';
MyString s3( s1 + " True!!!" );
cout << "'" << s3 << "'" << " has len " << s3.size() << '\n';
for( size_t i = 0; i < s3.size(); ++ i ) cout << s3[i];
cout << endl;
size_t i = s3.find( 'w' );
if( i != MyString::npos )
{
s3[i] = 'l';
s3[i+1] = 'u';
s3.erase( s3.size() - 7 );
s3 += "Ha! Haaahhh!!!";
for( size_t i = 0; i < s3.size(); ++ i ) cout << s3[i];
}
cout << "\nTesting using getline with split (splitting up line) ...\n";
MyString test_line = takeInMyString( "Enter a line of text: " );
vector< MyString > vms;
split( vms, test_line );
for( size_t i = 0; i < vms.size(); ++ i )
cout << "'" << vms[i] << "' ";
cout << endl;
cout << "\n\nTesting reading file with 7 word using fin >> MyStringVar in a loop ... \n";
std::ifstream fin( FNAME );
int count = 0;
if( fin )
{
MyString ms;
while( fin >> ms )
{
++count;
cout << " (" << count << ") " << ms;
}
fin.close();
cout << endl;
}
else cout << "Ther was a problem opening file " << FNAME << '\n';
cout << "\n\n";
do
{
MyString name = takeInMyString( "Enter ALL your names (include middle name) : " );
cout << "You entered: \"" << name << "\"\n";
toCapsOnAllFirstLetters( name );
MyString mid;
// erase 2nd word of more than 2 words ... IFF exist //
size_t i = name.find( ' ' );
if( i != MyString::npos )
{
size_t j = name.find( ' ', i+1 ); // start at i+1 //
if( j != MyString::npos )
{
mid = name.substr( i+1, j-i );
name.erase( i, j-i );
}
}
name = "Hi there, " + name + '!';
cout << name << '\n';
if( mid.size() )
cout << "Your middle name is " << mid << '\n';
// print after erasing 1st 3 char's ...
cout << name.erase(0, 3) << '\n';
// erase next middle word ... IFF exists //
i = name.find( ',' );
if( i != MyString::npos )
{
size_t j = name.find( ' ', i+2 );
if( j != MyString::npos )
name.erase( i, j-i);
}
cout << name << '\n';
}
while( more( "names" ) );
}