Programming Fundamentals 15
Programming Fundamentals 15
PROGRAMMING
C++ STRINGS
C++ STRINGS
• One of the most useful data types supplied in the C++ libraries is the string. A string is a
variable that stores a sequence of letters or other characters, such as
"Hello" or "May 10th is my birthday!".
Just like the other data types, to create a string we first declare it, then we can store a value in
it.
string testString; testString = "This is a string.";
We can combine these two statements into one line:
string testString = "This is a string.";
• Often, we use strings as output, and cout works exactly like one would expect:
cout << testString << endl;
In order to use the string data type, the C++ string header must be included at the top of the
program
• Also, you’ll need to include using namespace std; to make the short name string visible
instead of requiring the cumbersome std::string. (As a side note, std is a C++ namespace
for many pieces of functionality that are provided in standard C++ libraries.
• For the purposes of this class, you won't need to otherwise know about namespaces.)
Thus, you would have the following #include's in your program in order to use the string
type.
#include using namespace std;
BASIC OPERATIONS
COUNTING THE NUMBER
• Counting the number of characters in a string. The length method returns the number of
characters in a string, including spaces and punctuation. Like many of the string
operations, length is a member function, and we invoke member functions using dot
notation. The string that is the receiver is to the left of the dot, the member function we
are invoking is to the right, (e.g. str.length()). In such an expression, we are requesting the
length from the variable str.
ACCESSING INDIVIDUAL CHARACTERS
• Using square brackets, you can access individual characters within a string as if it’s a char
array. Positions within a string str are numbered from 0 through str.length() - 1. You can
read and write to characters within a string using [].
COMPARING TWO STRINGS
• You can compare two strings for equality using the == and != operators. Suppose you ask
the user for his or her name. If the user is Julie, the program prints a warm welcome. If
the user is not Neal, the program prints the normal message. Finally… if the user is Neal,
it prints a less enthusiastic response
APPENDING TO A STRING
• C++ strings are wondrous things. Suppose you have two strings, s1 and s2 and you want
to create a new string of their concatenation. Conveniently, you can just write s1 + s2, and
you’ll get the result you’d expect. Similarly, if you want to append to the end of string,
you can use the += operator. You can append either another string or a single character to
the end of a string
SEARCHING WITHIN A STRING
• The string member function find is used to search within a string for a particular string or
character. A sample usage such as str.find(key) searches the receiver string str for the key.
The parameter key can either be a string or a character. (We say the find member function
is overloaded to allow more than one usage).
EXTRACTING SUBSTRINGS
• Sometimes you would like to create new strings by extracting portions of a larger one.
The substr member function creates substrings from pieces of the receiver string. You
specify the starting position and the number of characters. For example, str.substr(start,
length) returns a new string consisting of the characters from str starting at the position
start and continuing for length characters. Invoking this member function does not change
the receiver string, as it makes a new string with a copy of the characters specified.
MODIFYING A STRING BY INSERTING AND
REPLACING
• Finally, let’s cover two other useful member functions that modify the receiver string. The
first, str1.insert(start, str2), inserts str2 at position start within str1, shifting the remaining
characters of str1 over. The second, str1.replace(start, length, str2), removes from str1 a
total of length characters starting at the position start, replacing them with a copy of str2.
It is important to note that these member functions do modify the receiver string
TOPUPPERCASE, TOLOWERCASE:
• These functions take a string, and return a new string with all letters in lower or upper
case respectively. These can be used to change two strings to a uniform case before
comparing to allow a case-insensitive comparison.