Please enable JavaScript to use CodeHS

More String Functions in C++

By David Burnham

As we saw in our Strings Basics in C++ tutorial, strings in C++ are mutable. This means we can change strings without having to entirely overwrite them like we would do in a language such Java.

We saw in our Strings Basics tutorial that we can do several common string manipulations, such as concatenating two strings, taking part of a string with a substring, looping through a string, and searching a string. In this tutorial, we are going to dive a little more into some things that we can do within the C++ language, such as inserting into, erasing from, and replacing letters directly in the string.

Referencing String Characters

Like other languages, strings in C++ are zero-indexed. This means that the first character in a string has an index of zero and the second has an index of one. We can access individual characters one of two ways:

string str = "Hello";
char first = str.at(0); // Assigns 'H' to first
char second = str[1]; // Assigns 'e' to second
Plain text

Both of these methods will return a valid character the same, however, using the [ ] is considered a safe access. If you try to access a character at an index that doesn’t exist with the [ ], you will not get anything in return. If you try to access the same invalid index with the .at() function, C++ will terminate your program and throw an error.

Play around with the example below to see this difference in action.

Updating Strings

As mentioned above, strings in C++ can be updated character by character instead of having to recreate the entire string. In a language such as Java where we can’t edit a string, replacing a character requires a substring for the first part, then the new character, then a substring for the remainder of the phrase.

In C++ we can change that one character directly using [ ] to access it.

string str = "Hello";
str[0] = 'M';

cout << str; // Prints Mello
Plain text

C++ also has three string functions that can come in hand to make modifications to a string:
str.erase(index, length) - Erase part of a string starting at the index for a specified length.
str.insert(pos, str2) - Insert another string (or char) at a given position.
str.replace(index, length, str2) - A combination of the above. Erase at an index for a specified length, then insert a new string.

Take some time to play around with the example below. Can you change the string using replace to replace broccoli with your least favorite vegetable?

Converting Strings to Numbers

Before we wrap up this tutorial, let’s take a look at one additional string task: converting strings into numbers. Converting between variable types is a pretty common task as we may often want to read in a line as a string, then convert parts of it to numbers so that we can perform basic calculations.

Converting strings to numbers requires that the start of your string is in fact a number. If this is not the case, C++ will throw an error.

Converting a string to a number uses the series of sto commands. sto is followed by a letter or two to signify the number that is being converted:

stoi - Converts to an integer
stod - Convert to a double
stol - Convert to a ling

Plus other less common conversions.

To convert the other way (number to string) we use the to_string() functions.

int num = 20;

cout << "My number is " + to_string(num);
Plain text

Check out the example of this below.

Your turn to try

In the program below, the user is prompted for the number of pizzas and the number of people. Both of these are read in as strings.

Convert them to numbers and then divide the number of pizzas by the number of people. Insert the answer into the string that is provided using the insert command.