// stringetc.cpp -- Methods for strings

#include <iostream>
#include <string>
using namespace std;  // This allows us to use the standard class string.

void main (void){
	string s1, s2, s3, s4;
	char c;
	int where, howMany;

    // Reading a string from standard input
	cout << "Enter a word: ";  
	cin >> s1;    // This will read a word (skipping blanks) into s1
	cout << "The string is \"" << s1 << "\" of length " << s1.length() << endl;
	      // The method length returns the number of characters in a string.
	      // Notice above the construct \"  the \ is an escape mechanism that tells
	      // the compiler that we want to print out the following character, the
	      // doublequote

	// Accessing the characters of a string
	cout << "Here are the characters of the word you have entered:" << endl;
	for (int i = 0; i < s1.length(); i++){
		c = s1.at(i);          // The method at returns the ith characters of s1
		cout << c;
	}

	cout << "\nEnter a word: ";
	cin >> s2;   
	cout << "The string is \"" << s2 << "\" of length " << s2.length() << endl;

	// String concatenation
	s3 = s1 + s2;  // The operator + when given two strings returns their 
	               // concatenation.
    cout << "The concatenation of \"" << s1 << "\" and \"" << s2 << "\" is \"" 
		 << s3 << "\"" << endl;

	// Inserting a new string into a given string
	cout << "Specify the word you want to insert in s3: ";
	cin >> s4;
	cout << "Specify the position where to do the insertion: ";
	cin >> where;
	s3.insert(where, s4); // It inserts s4 into s3 starting at position where
	cout << "The result of the insertion is \"" << s3 << "\"" << endl;

	// Replacing a substring of a string
	cout << "How many characters do you want to remove? ";
	cin >> howMany;
	cout << "At which position? ";
	cin >> where;
	cout << "Enter word word used to replace them: ";
	cin >> s4;
	s3.replace(where, howMany, s4);
	cout << "The result of the replacement is \"" << s3 << "\"" << endl;

	// Finding a word in another word
	cout << "Enter word you are looking for in s3: ";
	cin >> s4;
	where = s3.find(s4);  // find returns the position where s4 first (from left)
	                 // appears in s3, or -1 if there is no such occurrence.
	cout << "\"" << s4 << "\" occurs at position " << where 
		 << " in \"" << s3 << "\"" << endl;
}
/* The result of a run of this program is:
Enter a word: anna
The string is "anna" of length 4
Here are the characters of the word you have entered:
anna
Enter a word: maria
The string is "maria" of length 5
The concatenation of "anna" and "maria" is "annamaria"
Specify the word you want to insert in s3: alonzo
Specify the position where to do the insertion: 4
The result of the insertion is "annaalonzomaria"
How many characters do you want to remove? 3
At which position? 6
Enter word word used to replace them: ingargiola
The result of the replacement is "annaalingargiolaomaria"
Enter word you are looking for in s3: gar
"gar" occurs at position 8 in "annaalingargiolaomaria"
  */
