The C String Library - string.h (aka cstring)

Strings as Character Arrays

Before C++ and classes, strings were stored in simple arrays of characters. Thus,
char name[12] = "Bob Dylan";
would be a simple way to create a string with an initial value. In memory, this array looks like:
| B | o | b |   | D | y | l | a | n | \0 |   |   |
The only thing special about this array is that an extra character, the nul character tells us where the string's value ends.

Note: In this case, the array is actually bigger than the string held in it.

Accessing/Changing Strings

Because a string is really just an array, we must access or change it by dealing with each element (like other arrays).

This means it becomes tedious to perform common string operations, i.e.,

To change the value of a string,
we must change several characters individually.
To find the length of the string,
we must search for the nul character.

What kind of language construct would we use to manipulate or search through the characters of a string?

Because these are such common operations, there are library functions that do these things for us...


The library string.h (also referred to as cstring) has several common functions for dealing with strings stored in arrays of characters. Following are the 4 basic string library functions that we'll discuss:

The C String Library - string.h (aka cstring)
Copyright © 1993-2000 by Robert I. Pitts <rip at bu dot edu>. All Rights Reserved.