Strings


A string is a sequence of characters but do you know how it is implemented? This can be considered an example of an abstract data type. You probably have an idea of what you can do with a string, but how it is implemented in memory is "hidden" from you.

Some Ways String Are Implemented


   
Pascal

Strings are implemented as an array of characters with the first character treated as a 1-byte integer giving the length of the string. Thus Pascal strings are limited to a maximum of 255 characters.



Java

In Java strings are implemented as the String class with a variety of functions which can be used to manipulate the string.



C/C++

Strings are implemented as an array of characters. The end of a string is marked with a null terminator (ASCII character zero). Functions to manipulate the Kernigan and Ritchie (named for the authors of C) strings are defined in the header file string.h



C++

The C++ Standard Template Library includes a template class for strings with a variety of functions which can be used to manipulate the string. Functions to manipulate the C++ template strings are defined in the header file string (note that the .h is omitted and you must also add after the #include statement, the statement using namespace std;



A Sampling of Functions for Manipulating
Kernighan and Ritchie String Arrays

To use any of these functions you must...

#include <string.h>



The string class of the Standard Template Library

To use any of these functions you must...

#include <string> // Note: this is not the same as . It includes much more.
using namespace std;