LPString concat Solution

The append and concat functions combine the text of two LPString objects into a single string. The difference between them is how they store and return the combined text. The append function modifies this string, adding the text in the parameter to it. The concat function doesn't change either string; instead, it creates a new LPString object with the combined text of the original strings. Two distinct solutions demonstrate fundamentally different ways of implementing the concat function. The first implementation uses the same basic array notation demonstrated throughout the LPString class. The second, more compact implementation uses other, previously completed member functions. Both implementations follow the steps outlined in Figure 18.

LPString LPString::concat(const LPString& s) const
Two LPStrings represented as a sequence of boxes: 'Hello' and ' world' (notice the space at the beginning of the second string).
concat validation. The problem begins with two LPString objects, this and s. The "const" keyword inside the parentheses prevents the function from modifying s, while the "const" at the end prevents it from modifying this. The function's first task, step 1, is ensuring that the combined strings will fit (i.e., not overflow) a new LPString object:
if (text[0] + s.text[0] >= LENGTH)
	throw "strings too long to concatenate";
This step is the same for both implementations.
A picture illustrating two LPString objects, 'this' and 'local', represented as a sequence of boxes with each string's length in the first box. The function copies elements 0 through 5 from 'this' to 'local'.
Array VersionMember Function Version
LPString local;				// (a)
for (int i = 0; i <= text[0]; i++)	// (b)
	local.text[i] = text[i];
LPString local(*this);		// (c)
//LPString local = copy();	// (d)
 
concat steps 2 and 3: create and initialize local. The program creates an LPString object named local in the concat function's scope and copies the characters saved in this to it.
  1. An automatic or local variable definition.
  2. The for-loop copies this to local. Starting the loop at 0 copies the length and the text.
  3. The copy constructor creates local, dereferences this, and copies the contents of the dereferenced string to the local variable.
  4. An alternate to (c): defines local as in (a) but uses the copy function, written in the previous section, to copy this to it.
A picture illustrating two LPString objects, 's' and 'local', represented as a sequence of boxes. The function copies elements 1 through 6 from 's' to 'local'.
Array VersionMember Function Version
for (int i = 1; i <= s.text[0]; i++)		// (a)
	local.text[text[0] + i] = s.text[i];
local.text[0] += s.text[0];			// (b)
local.append(s);		// (c)

 
concat steps 4 and 5: append s to local and update its length. The function finishes the concatenation operation by appending s to the end of local.
  1. The for-loop starts at 1 and iterates once for each character in s. text[0]+i creates an index in local, beginning at its end.
  2. Sums the lengths of the two strings and saves the sum as the new length of local.
  3. We previously created the append function and use it here to append s to the end of local.
Both version finish by returning local: return local;.

Array Version

LPString LPString::concat(const LPString& s) const	// fundamental operations solution
{
	if (text[0] + s.text[0] >= LENGTH)		// total length may not exceed LENGTH
		throw "strings too long to concatenate";

	LPString local;

	for (int i = 0; i <= text[0]; i++)		// copies this string
		local.text[i] = text[i];

	for (int i = 1; i <= s.text[0]; i++)		// appends s to local
		local.text[text[0] + i] = s.text[i];
	local.text[0] += s.text[0];			// updates local's length

	return local;
}

Member Function Version

LPString LPString::concat(const LPString& s) const	// Existing functions solution
{
	if (text[0] + s.text[0] >= LENGTH)		// total length may not exceed LENGTH
		throw "strings too long to concatenate";

	LPString local(*this);				// calls the copy constructor
	//LPString local = copy();			// alternate for the copy constructor
	local.append(s);

	return local;
}
Complete versions of the concat function.