//
// Program name:  play_with_strings
// Author:  B. Massingill 
//
// Purpose:  try out some functions of the string class
//
// This program is structured as a bunch of little functions (each
// trying out one or more things that can be done with strings).  
// The main program then calls each of the functions in turn (calling 
// some of the more "interesting" ones twice.)  It is easy to change the
// main program to just call the functions you find of most interest.
//
// Input / Output:  
//	see program
//
#include <iostream.h>
#include <string>

// stream input, output
void streamIO(string &s1, string &s2)
{
	cout << "Enter two strings, ending each string "
		<< "with some whitespace\n";

	cin >> s1 >> s2;

	cout << "s1 = " << s1 << endl;
	cout << "s2 = " << s2 << endl;

	return;
}

// comparison operators
void comparisons(const string s1, const string s2)
{
	if (s1 < s2)
		cout << "s1 < s2\n";
	if (s1 == s2)
		cout << "s1 == s2\n";
	if (s1 > s2)
		cout << "s1 > s2\n";
	return;
}

// length
void tryLength(const string s1, const string s2)
{
	cout << "length of s1 = " << s1.length() << endl;
	cout << "length of s2 = " << s2.length() << endl;
	return;
}

// concatenation
void concat(const string s1, const string s2)
{
	string s3 = s1 + s2;
	cout << "concatenating s1 and s2 gives s3 = "
		<< s3 << ", with a length of " << s3.length() << endl;
	return;
}

// line-oriented input
void lineIO(string &s1)
{
	string tempS;
	cout << "Now enter a line of text (hit enter at end)\n";
	// first discard any previous input, up to end of line
	// this is only necessary because we previously read things
	//	from cin with the ">>" operator
	cin.ignore(10000, '\n');
	getline(cin, s1);
	cout << "s1 (in single quotes) = '" << s1 << "'\n";
	return;
}

// substr
void trySubstr(const string s1)
{
	string::size_type pos, lng;
	string tempS;
	cout << "trying substr() function\n";
	cout << "enter starting position (must be less than " 
		<< s1.length() << ")\n";
	cin >> pos;
	if ((pos < 0) || (pos >= s1.length()))
	{
		cout << "invalid; assuming 0\n";
		pos = 0;
	}
	cout << "enter length of substring (must be less than "
		<< s1.length() - pos << ")\n";
	cin >> lng;
	if ((lng < 0) || (lng >= s1.length()-pos))
	{
		cout << "invalid; assuming 1\n";
		lng = 1;
	}
	tempS = s1.substr(pos, lng);
	cout << "s1.substr(" << pos << ", " << lng << "), "
		<< "in single quotes, = '" << tempS << "'\n";
}

// find
void tryFind(const string s1)
{
	string::size_type pos;
	string tempS;
	cout << "trying find() function\n";
	cout << "enter text to find in s1, ending with whitespace\n";
	cin >> tempS;
	pos = s1.find(tempS);
	if (pos != string::npos)
		cout << "text found at position " << pos << endl;
	else
		cout << "text not found\n";
	return;
}


int main()
{
	string s1, s2;

	cout << "This program shows how some functions of the string "
		<< "class work\n";

	streamIO(s1, s2);

	comparisons(s1, s2);

	tryLength(s1, s2);

	concat(s1, s2);
	
	lineIO(s1);

	trySubstr(s1);		// let's do this twice, seems interesting
	trySubstr(s1);

	tryFind(s1);		// do this twice as well
	tryFind(s1);

	return 0 ;
}