Return-by-reference (notice the variable "a" used here is defined in local or function scope, so the supplier function needs the static keyword to operate correctly. But the following string examples define the corresponding variables in class-scope, so they are NOT deallocated when the function returns)
Larson (1983) lists a dozen general problem-solving heuristics or techniques (p. 1):
search for a pattern
draw a figure
formulate an equivalent problem
modify the problem
choose effective notation
exploit symmetry
divide into cases
work backward
argue by contradiction
pursue parity
consider extreme cases
generalize
Although his heuristics focus on mathematical problems, computer scientists also use them. We use some of his techniques, for example, "formulate an equivalent problem," when working on advanced theoretical problems, but we use many others in our day-to-day programming. The following discussion and the associated examples focus on one beneficial technique: "draw a figure."
Most people need some way to think about or "see" a problem. Jonassen (2000) explains that "problem solving requires the mental representation of the situation in the world. That is, human problem solvers construct a mental representation (or mental model) of the problem, known as the problem space. Problem solving requires manipulation of the problem space, be it an internal mental representation or an external physical representation" (p. 65). Pictures serve as "representations" that help us understand what we know about a problem, what we need to find, and the relationships between the parts. For example, in the pouring puzzle problem, a picture helped us understand and develop an algorithm for pouring water between two glasses.
We "manipulate the problem space" by changing the picture to reflect each problem-solving step. We can change the picture in many ways depending on how or where we draw it and how we wish to use it. If we draw the picture on a whiteboard, it's easy to erase and redraw parts as the solution progresses. Alternatively, when we use pictures to help write code, drawing a sequence of pictures is often convenient, each picture representing one step in the problem's solution. This approach allows us to refer to a specific picture as we write the corresponding code.
Drawing a picture to help solve a problem is a surprisingly powerful technique. Unfortunately, there are too many problems with too many variations to allow us to create a general drawing algorithm or recipe. Drawing and using a picture to help solve a problem is a learned skill, but without a recipe, we must learn it through extended practice. Fortunately, there is a process, admittedly general, to guide us:
Study how an experienced problem solver draws and uses pictures to solve a problem
Draw pictures for a similar problem and use them to solve it
Compare your pictures and solution to those of the experienced problem solver
To practice our drawing and problem-solving skills, we need a set of practice problems. We can view strings as an abstract data type, which we can implement in a variety of different ways. Although each implementation is a different problem, we can capitalize on their similarity to reduce the time needed to understand the basic problem. To this end, we outline a general string and then implement it through three examples. The examples provide practice drawing and using pictures to help solve problems, giving us more experience using arrays and further demonstrating how to create and use classes in C++ programs.
Modern operating systems and applications strive to accommodate a worldwide audience. However, detecting and utilizing an appropriate natural or human language occurs at a high level within a software system. Implementing a string type and its supporting operations takes place at a much lower level. Once the software system identifies an appropriate natural language, it selects and uses an appropriate low-level string implementation. The textbook is written and predominantly used in an English-speaking country. So, for simplicity, the following description and implementations assume a natural language that reads left to right.
Developing A String Data Type
Although early high-level programming languages like FORTRAN and ALGOL didn't include a string type when first introduced, today all general-purpose programming languages do. Independently of how we represent strings, they consist of multiple related but distinct data elements. In an object-oriented language, we can easily group the elements in a class, but they present an authentic challenge for non-object-oriented languages.
Most string operations - the services a string object provides to client programs - are independent of the string's implementation. They describe what the string functions do without specifying how they do it. The following list of proposed string functions is not exhaustive but is sufficient to practice drawing and problem solving and to review arrays, classes, and their associated notations.
Jonassen, D. H. (2000). Toward a design theory of problem solving. Educational Technology, Research and Development, 48(4), 63-85.
Larson, L. C. (1983). Problem-Solving Through Problems. New York: Springer-Verlag.