Strings

As briefly covered when going over our “hello world” program, a string is any sequence of characters surrounded by quotes. For example: “Hello world!”, “a”, and “1234” are all strings.

To be able to create string variables, we need to #include <string> at the top of our program (just like we do for vector and iostream). After that, we can create variables of the string data type just like we do for variables of any other data type. Simply write the data type (std::string in this case) followed by the variable’s name and the value to assign to the variable:

#include <iostream>
#include <string>

int main() {
    std::string hello = "Hello world!\n";
    std::cout << hello;
    return 0;
}

String Concatenation

By using the plus sign, we can easily “concatenate,” or combine, multiple strings together:

#include <iostream>
#include <string>

int main() {
    std::string hello = "Hello ";
    std::string world = "world!\n";
    std::cout << hello + world;
    return 0;
}

Getting a Single Character

By using the square brackets (“[” and “]”) just like we do with arrays and vectors, we can get single characters from a string. Just follow the name of the string by square brackets containing the index of the string to get a character from (where an index of 0 represents the first character, an index of 1 represents the second character, etc.):

#include <iostream>
#include <string>

int main() {
    std::string hello = "Hello world!";
    std::cout << hello[3] << "\n";
    return 0;
}

Here, hello[3] represents the forth character in the string (which is “l”).

Some Useful Functions

The table below contains some of the more commonly used string functions. For a more complete list, click here.

(The examples column below references a string called “str” equal to “Hello world!”.)

NameDescriptionExample
sizeGets the number of characters inside the stringstr.size() is equal to 12.
clearClears all of the string’s contentsstr.clear();

New string: “”
insertInserts a string (at given index)str.insert(11, "!!");

New string: “Hello world!!!”
eraseRemoves a given number of characters from the string (starting at given index)str.erase(5, 6);

New string: “Hello!”

Explanation: Starting at index 5, 6 characters are removed.
push_backAdds a character to end of stringstr.push_back('!');

New string: “Hello world!!”
pop_backRemoves last characterstr.pop_back();

New string: “Hello world”
substrGets a given number of characters from the string (starting at given index)std::string str2 = str.substr(6, 5);

str2: “world”

Explanation: Starting at index 6, the next 5 characters are taken from str.

Challenge Problem

Write a program that can determine if a string is a palindrome (the same forwards and backwards).

For example, given the string “racecar”, the program should print “racecar is a palindrome.” to the screen. Given the string “hello”, the program should print “hello is not a palindrome.” to the screen.

Hint: You’ll need to use a for or while loop to check each character in the string.

If you get stuck, check out my solution here.