Essentialssec13.htm

Essentials of C++: Section XIII


Strings

As we saw in Section VIII, Part C, strings in C++ have traditionally been represented as character arrays with the null character used to mark the end of each string. While there was no built-in string type in the original definition of C++, there were a number of string processing functions provided as part of the standard library and declared in the header file 'string.h'. The more common of these include:

char* strcat(char *dest, const char* src);
     appends src to dest and returns dest.

int strcmp(const char* s1, const char* s2);
     compares the two strings, s1 and s2.
        If s1 < s2 (if s1 is before s2 alphabetically),
          a value less than 0 is returned.
        If s1 == s2, the value 0 is returned.
        If s1 > s2), a value greater than 0 is returned.

char* strcpy(char* dest, const char* src);
        copies src to dest and returns dest.
        (Given that the strings used here are arrays
         which are manipulated as pointers to memory,
         it does not, in general, work to simply assign one
         string to another using the assignment operator.
         Click here for a more detailed explanation of the issues involved.)

int strlen(const char* s);
        returns the length of s, not
        counting the null character.

Note that all these functions, as well as the many others described in C++ manuals, assume the existence of the null character and have unexpected results if the null character is not found.
 

B. The C++ Extension for Strings
More recently, a string class has become part of the ANSI C++ standard. The class name is string and it is possible to declare strings as you would declare any other class instance if you include the header file 'cstring.h'. For example:

string s1;
string s2;
string s3;

There is also a complete set of member functions and overloaded operators to handle string manipulation. The most common member functions include:

string& append(const string& s);
     equivalent to strcat

int compare(const string& s1);
     equivalent to strcmp

int length();
     equivalent to strlen.

string (const string& s);
     the copy constructor as in:
        string s ("abcd");

Since these are member functions, one string parameter is implicit and they are all called the same way any other member function is called. Given the string declarations above, the following would be valid uses of these functions:

int len = s1.length();
s3 = s1.append(s2);
if (!s1.compare(s2))   // if s1 is equal to s2 - 'compare' returns 0 if the strings are equal
{
   // code goes here
}

Common string operators include:

string& operator = (const s)
     as in s2 = s1;
        copies s1 to s2, replacing what was in s2.

string& operator+= (const string& s);
     as in s2+=s1;
        concatenates s1 to s2, equivalent to append.

friend int operator==  (const string& s1, const string& s2);
          as in if (s1 == s2)........
                compares to strings. If they are equal, 1 is returned;
                if they are not equal, 0 is returned.

friend int operator< (const string& s1, const string& s2);
         as in if (s1 < s2) ......
                Returns 1 if s1 < s2; otherwise returns 0.

 

Links to 'The Story of C++" and other documents

Basic Discussion of Strings
Issues in Copying Strings

Top of Section Main Menu of Essentials of C++ Next Section