Constructing a string using character array

In C++, a string can be composed by using an array of characters. When we compose a string using an array, we have to reserve a space to store a NULL character (\0) at the last array's element to indicate the end of the string. Suppose we want to create a string variable containing a name of a person called James; we need an array with at least six elements, since James is composed of five characters. Please take a look at the following diagram:

As we can see in the preceding diagram, we need an array with at least six elements to store a string containing five letters. There are several ways to ...

Get C++ Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.