Section 6 - Varieties of Strings
1A.6.1 C Strings and S-C Strings
C++ provides two major ways to handle character strings.
- The newer string class (s-c strings)
- The older NULL-terminated array of chars (C Strings).
Although I say s-c strings are "newer", this is relative. They have been around for over 15 or 20 years, but they came after original C Strings which were part of the C language, the predecessor to C++.
I am going to use s-c strings whenever feasible in this course because they are much easier to work with. We will need to know C Strings later in course because they offer some speed advantage and surgical flexibility over s-c strings, and when we get to that point, I'll review C Strings for you.
If you are unsure of what kind of strings you were using in your original course, here is a sample of the different ways each is handled. I'm sure you'll recognize the syntax in one or the other column. This is a good reference for the common operations like string comparison and length.
Operation | C String | s-c string |
---|---|---|
Declaration | char s1[100], s2[100]; | string s1, s2; |
Assignment | strcpy(s2, "hi dad"); | s2 = "hi dad"; |
Length | strlen(s1) | s1.length() |
Comparison | strcmp(s1, s2) | s1.compare(s2); s1 < s2, s1 > s2, s1 == s2 |
1A.6.2 C String Fundamentals
Although we are going to use s-c strings, we still have to understand C Strings at the most fundamental level. (I will use the shorter term CString to mean a C String). A CString is an Array of chars:
char name[80];
However, name, is not quite a CString. For name to be a CString we must use it in a special way: Whenever we put char data into the array, we must terminate the data with the NULL char, '\0'. This NULL (or informally, null) tells all CString functions where the CString ends.
To initialize a CString in the declaration, and also set its size automatically, we omit the size (80 above) and use this notation:
char name[] = { 'C', 'h', 'e', 'r', 'r', 'i', 'e', 's', '\0' };
This creates an char array of length 9. It is more common, however, to use the shortcut:
char name[] = "Cherries";
which places the eight letters of the word "Cherries" in locations name[0],...name[7], and a null in name[8].
As with any array, we can access an individual character of a CString anywhere in the program using brackets:
name[0] = 'H'; name[1] = 'i'; name[2] = '\0'; // must null-terminate if we use manual notation
And there is also the shortcut for the above sequence:
strcpy(name, "Hi");
However, you absolutely cannot do this with CStrings:
name = "Hi"; // error!
1A.6.3 S-C Strings
As I said a moment ago, we won't use CStrings much because s-c strings are more useful. Unless stated otherwise, I will use the word string to mean an s-c string. We declare a string variable just as we would an int, except we use the word string instead of int:
string thxMom;
Strings, unlike ints, are not primitive data, but part of a pre-defined class. The good news is, C++ lets you treat string objects almost as though they are primitive data types. So you can, in fact do the following:
thxMom = "Thanks, Mom!";
It is incredibly easy to use strings. Here is a simple example. You'll notice that we must #include <string> at the top.
#include <iostream> #include <string> using namespace std; int main() { string str; str = "Hello there. C++ is easy."; cout << str << endl; return 0; }
1A.6.4 The Concatenation Operator
The concatenation operator is used to join together two or more strings.
string acceptanceSpeech, thxFox, outputString; acceptanceSpeech = "I'd like to thank ";` thxFox = "everyone at Fox and FBC."; outputString = "First of all " + acceptanceSpeech + thxFox;
We can concatenate two strings as long as at least one of them is a string variable. In the above example we concatenated a string literal, "First of all " with the string variable acceptanceSpeech. You cannot directly concatenate two string literals. In other words, this is illegal:
outputString = "First of all " + "I'd like to thank "; // error!
However, there is a trick that will allow you to use the concatenation operator on two literals:
outputString = string("First of all ") + string("I'd like to thank "); // works!
This trick works because the construct string("First of all ") creates a temporary string object out of the literal "First of all ". This is an example of an anonymous object. Anonymous objects are a quick and dirty way (in many languages including Java and C#) to create an object of any class for use in one expression, where we don't need to reuse that object later in the program. You simply use the class constructor as if it were a method that returns the object (which, in a way, it is). Do you recall other places where anonymous objects are commonly used?
Study the above expression so that you understand this concept. It gives you a way to convert any CString into a string. For example:
// Here is the CString "A" char strVal[2]; strVal[0] = 'A'; strVal[1] = '\0'; // convert from CString to string string retVal; retVal = string(strVal);