computer science II
c m s c 214  
f a l l   2 0 0 1  

C++-style Strings

© by Charles Lin. All rights reserved. You must receive written permission from Charles Lin to reproduce this webpage in any form.

C-style string predates C++ strings. From CMSC 114, you should know that a C-style string has a "char *" type, and the string ends in a null character. Unfortunately, because of this, C-style strings are harder to use than other simple types in C. In particular, you must use the following functions:

and many other string functions.

This solution is due to the fact that strings can be arbitrary in size, where int's, float's, bool's, and char's have fixed sizes.

Wouldn't it be much better if strings were a class, where assignment and == are correctly overloaded to do what you expect them to, and wouldn't it be nice if string objects could manage the memory needed.

That's what the developers of C++ standard library thought. Therefore, C++ has a string class.

The string class should be very easy to learn. Yet, students often make their lives more complicated by trying to treat C++ like strings as C strings. Don't do this!

Let's go through the basic operations of a C++ string.

Declaration

#include <string> // need this for C++ strings

int main()
{
  string str;   // str contains empty string
  string str2 = "cat";   // str2 contains cat
  string str3 = str2;   // str3 contains cat
}
Before you can use the C++ strings, you must include the library. Notice that you include <string> instead of <string.h>.

Be careful about making any assumptions on C++ strings. In particular, you should not assume that such strings end in a null character.

Look at the declaration above. Notice that "str2" is assigned to "cat". "cat" is a C-style string, not a C++-style string. So, obviously, there is a constructor in the string class that looks like:

  string( const char *other );  // Constructor for string
Of course, you can copy one C++ string object to another (see "str3" above).

Reading in strings

  string s;
  cin >> s;  // Stores string from standard input
Notice that you don't have to worry about allocating memory. The string class automatically handles this for you. The semantics of reading into a string should be: I have seen buggy versions, however, where reading into a C++ string consumes whitespace AFTER reading in the string. That is, more than likely, an incorrect implementation of the input operator on a string.

Comparing strings

To compare two strings, you do NOT use strcmp. In fact, using strcmp shouldn't work (why?). Instead, you use ==.
  string s1 = "cat", s2 = "cat";

  if ( s1 == s2 )
     cout << "same" << endl;
This performs a case-sensitive comparison of the two string objects. Recall why this doesn't work for C-style strings. (Answer: C-style strings use char * pointers, so using == compares pointer addresses, not the string content).

Printing strings

Very simple.
  string s1 = "cat";

  cout << s1 << endl;

Length of a string

There are two ways to determine the length of a string. You can use the "length" method or the "size" method. Both will return an int, which is the length of a string.
  string s1 = "cat";

  // Both print 3, the length of "cat"
  cout << s1.length() << endl;
  cout << s1.size() << endl;

Accessing individual characters

The [] operator has been overloaded to access individual characters. In general, you should try not to modify the characters of a string, although it is permitted.
  string s1 = "cat";

  // Print 'a'
  cout << s1[ 1 ] << endl;

Concatenation

+ has been overloaded to mean concatenation. Thus,
  string s1 = "cat", s2 = "house", s3;

  s3 = s1 + s2;
  // Print 'cathouse'
  cout << s3 << endl;
You don't use "strcat" to concatenate using C++-style strings.

Be careful!

Again, try not to fall in the trap of treating C++ strings like C strings. When thinking about whether you can use a certain C string function (such as strcmp, strcpy), ask yourself if this makes sense with C++-style strings. More than likely, it will not.

There's more

There are actually more functions for C++ strings, but the above should be enough.

Just because C++-style strings are easier to use than C-style strings, don't believe that you can simply forget C-style strings. Many functions in C primarily rely on C-style strings, especially those dealing with network applications. Also, such basic things as "argc" are still C-style strings. Unfortunately, you can't exactly wipe out C-style strings. However, when you don't need to use them, it's better and easier to use C++-style strings.

There is one drawback to C++-style strings. They are templates. Because of this, they can produce horrible looking error messages. Hopefully, compilers will be smart enough one day to avoid having such verbose error messages, but until then, you have to deal with it.

Web Accessibility